|
Hello Everyone...i'm creating a code that will scan LDAP and Active Directory for a fax number given to users and then it will compare both servers and will generate a text file with the discrepancies on both servers(only the numbers that don't match). The program seems to work but for some reason in the output i get a couple of users who have the same fax number in both servers. i wonder if i created the foreach loops correctly...can someone please guide me?????
namespace fax
{
// This sorted collection provides easy access to the FaxEntry objects added to it
// It also overrides ToString() to facilitate easy writing of results to the text file
public class FaxEntries : SortedList<string,>
{
System.Text.StringBuilder sb = new StringBuilder();
public override string ToString()
{
// If list is not already built up
if (sb.Length == 0)
{
// header
sb.AppendLine("XPID\t\tDisplay Name\tLDAP Fax#\tAD Fax#");
sb.AppendLine("-------------\t--------------\t-------------\t-------------");
// Iterate the collection and output each FaxEntry object via it's (overrided) ToString() method
foreach (KeyValuePair<string,> CurrItem in this)
sb.AppendLine(CurrItem.Value.ToString());
}
// Return the results
return sb.ToString();
}
}
public class FaxEntry
{
public string XPID;
public string DisplayName;
public string LDAPFaxNumber;
public string ADFaxNumber;
public FaxEntry(string XPID, string DisplayName, string LDAPFaxNumber, string ADFaxNumber)
{
this.XPID = XPID;
this.DisplayName = DisplayName;
this.LDAPFaxNumber = LDAPFaxNumber;
this.ADFaxNumber = ADFaxNumber;
}
// create an easily readible one line entry for the result
public override string ToString()
{
return Pad(XPID) + Pad(DisplayName) + Pad(LDAPFaxNumber) + Pad(ADFaxNumber);
}
// Add an appropriate number of tab characters to create a nice looking display to the passed string
string Pad(string ToPad)
{
if (string.IsNullOrEmpty(ToPad))
return "\t\t";
return ToPad + (ToPad.Length < 8 ? "\t" : string.Empty) + "\t";
}
}
class Program
{
static FaxEntries Entries;
static void Main(string[] args)
{
// The two properties to compare
string ComparisonProperty2 = "facsimiletelephonenumber";
string ComparisonProperty1 = "fax";
// The two servers to attach to LDAP/Unix and AD
string Server1 = @"LDAP://";
string Server2 = @"LDAP://";
Entries = new FaxEntries();
// Call compare to perform the comparison
Compare(Server1, ComparisonProperty1, Server2, ComparisonProperty2);
// Write the results to a file
try
{
System.IO.File.WriteAllText(@"C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\fax.txt", Entries.ToString());
// Write the results to the command line
Console.Write(Entries.ToString());
// Open a blank email
sendMyMail();
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}
}
/// ////////////////////////////////////////////////////////////////////////
static void sendMyMail()
{
try
{
MailAddress from = new MailAddress("rillo@company.com");
MailAddress to = new MailAddress("mo@company.com");
MailMessage message = new MailMessage(from, to);
message.Body = "AD and LDAP lbpcsfax Discrepancies Report";
message.Body += Environment.NewLine;
message.Body += Environment.NewLine;
try
{
message.Attachments.Add(new Attachment(@"C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\fax.txt"));
}
catch (Exception e1)
{
message.Body += "Will not attach " + Entries + " because " + e1.Message;
}
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "AD and LDAP fax Discrepancies Report";
message.Priority = MailPriority.Normal;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient("mailhost.company.com");
try
{
client.Send(message);
}
catch (Exception exx)
{
Console.WriteLine("Error sending email " + exx.GetBaseException() + " " + exx.InnerException);
}
message.Dispose();
}
catch (Exception e)
{
Console.WriteLine("Error sending email " + e.GetBaseException() + " " + e.InnerException);
}
}
/// ////////////////////////////////////////////////////////////////////////////////////
static string GetPropertyForDisplay(SearchResult result, string Name)
{
if (result == null)
return string.Empty;
ResultPropertyValueCollection Items = result.Properties[Name];
if (Items != null)
{
if (Items.Count == 1)
return Name + " = " + Items[0].ToString();
else
{
string ret = Name + " = ";
foreach (object myKey in Items)
if (myKey != null)
ret += "\t" + myKey.ToString();
return ret;
}
}
return string.Empty;
}
static string GetProperty(SearchResult result, string Name)
{
if (result == null)
return string.Empty;
ResultPropertyValueCollection Items = result.Properties[Name];
if (Items != null)
{
if (Items.Count == 1)
return Items[0].ToString();
else
{
string ret = "";
foreach (object myKey in Items)
if (myKey != null)
ret += "\t" + myKey.ToString();
return ret;
}
}
return string.Empty;
}
static void Compare(string Server1, string PropertyToCompare1, string Server2, string PropertyToCompare2)
{
// Create the new FaxEntries Collection, one for each entry we find on either server property
Entries = new FaxEntries();
// Go get the matching LDAP users
SearchResultCollection server1Results = GetListByProperty(Server1, PropertyToCompare1, "uid");
SearchResultCollection server2Results = GetListByProperty(Server2, PropertyToCompare2, "sAMAccountName");
//added
if (server1Results != null)
{
foreach (SearchResult CurrItem in server1Results)
{
string sAMAccountName = GetProperty(CurrItem, "uid");
Entries.Add(sAMAccountName.ToLower(), new FaxEntry(sAMAccountName, GetProperty(CurrItem, "givenName"), GetProperty(CurrItem, PropertyToCompare1), null));
}
}
else
{
Console.WriteLine("server1Results returned nothing");
}
if (server2Results != null)
{
// Repeat on the second server with the second property and compare, adding as necessary
foreach (SearchResult CurrItem in server2Results)
{
string sAMAccountName = GetProperty(CurrItem, "sAMAccountName");
if (Entries.ContainsKey(sAMAccountName.ToLower()))
{
Entries[sAMAccountName.ToLower()].ADFaxNumber = GetProperty(CurrItem, PropertyToCompare2);
}
else
{
if (!Entries.ContainsKey(sAMAccountName.ToLower()))
{
Entries.Add(sAMAccountName.ToLower(), new FaxEntry(sAMAccountName, GetProperty(CurrItem, "givenName"), null, GetProperty(CurrItem, PropertyToCompare2)));
}
}
}
}
else
{
Console.WriteLine("server2 Results returned nothing");
}
}
static SearchResultCollection GetListByProperty(string LDAPPath, string PropertyToPerformComparisonAgainst, string userTag)
{
DirectoryEntry entry = new DirectoryEntry(LDAPPath);
DirectorySearcher usersearch = new DirectorySearcher(entry);
string myFilter = string.Empty;
entry.AuthenticationType = AuthenticationTypes.None;
if (userTag.ToLower().Equals("uid"))
{
myFilter = "(&(" + userTag + "=*)(" + PropertyToPerformComparisonAgainst + "=*)(objectClass=lborgperson))";
}
else
{
myFilter = "(&(" + userTag + "=*)(" + PropertyToPerformComparisonAgainst + "=*))";
}
Console.WriteLine(LDAPPath);
Console.WriteLine(myFilter);
try
{
usersearch.Filter = myFilter;
SearchResultCollection result = usersearch.FindAll();
Console.WriteLine(result.Count.ToString());
return result;
}
catch (Exception ex)
{
Console.WriteLine("Exception caught:" + ex.InnerException + " hmm " + ex.GetBaseException());
}
usersearch.Dispose();
entry.Close();
entry.Dispose();
return null;
}
}
}<
|
|
|
|
|
You'll get a better response if you use <pre></pre> tags around your code.
FYI: LDAP is a protocol and it is also used by AD
|
|
|
|
|
There's an option in Crystal Report like "To set up record selection using Select Expert"........but I notice that I would be able to only hard-code the values that are already existing in the database field.........rather I'm looking for something where I can get the value selected directly from the combobox..........am I clear with those lines.........I need help on this............TIA
|
|
|
|
|
you can use a parameter field, and set it at runtime, you could use a forumla field and change the formula text at runtime to return your value, or you could find a new product to use, i'm traped in the same place you are, trying to make CR dynamic...i've managed to an extent thanks to several online examples and "Crystal Reports .NET Programming" by Brian Bischof(not sure about the first name) but a lot of the features that made it so great for programmers have been removed in later versions the version for vs 6 you could almost build a whole report at runtime, and in 2008 you have to struggle to change anything at runtime, espically with any kind of chart. If you find a good alternative to CR please let me know.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Thanks for your reply...........i understand wht to do............but i think i lost my field explorer view.........wht to do now.........i'm unable to bring it back..........pls help
|
|
|
|
|
um...view->field explorer? it could be a tab on the side mabye...i dont know what your in or how it was set up before...i cant really help with that.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hello!
I have a question about retrieveing a toolstripmenuitem from a contextmenustrip.
In a certain function I do the following:
contextmenustrip.Show();
After that I would like the function to be halted until one of the toolstripmenuitems is clicked and this item is the returned into the funtion.
So I don't want to use an event but rather something like one can do with forms:
if (this.TestForm.ShowDialog(this) == DialogResult.OK)
Is something similar possible with contextmenustrips?
|
|
|
|
|
As far as I am aware their is no way to emulate a ShowDialog method for a menu. If their is a such a way you would have to work with Windows API or advanced menu events.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios
Discounted or Free Software for Students:
DreamSpark - downloads.channel8.msdn.com
MSDN Academic Alliance - www.msdnaa.com
|
|
|
|
|
I have to read a large quantity of bianry data from a file and then store this data in an XML tag. There is approximately 8 MB of data.
This can be slow just to read the 8MB into memory using StreamReader.
I tried reading sayy 500k at a time. But i still have the issue of storing 8MB into memory.
If i would have just read 500k and then write 500k, i would be ok. But i am using XMLTextWriter that does not allow me to write tag data piece by piece. I have to have all the data and then write it to tag.
Any suggestions on how to handle this effectively will be appreciated.
Thanks,
cw
|
|
|
|
|
if you are just saving binary to XML you'll double the size of your data. If you encode it to base64 it'll only be about 4/3 the orginal size, but with conversion overhead.
Why do you need 8MB of binary data in XML anyway? To me it sounds like a design flaw not a memory issue.
-Spacix
All your skynet questions[ ^] belong to solved
I dislike the black-and-white voting system on questions/answers.
|
|
|
|
|
I actually do encode to base64. It does not even get that far as it is stuck reading all the data from the file.
The reason i need to do it this way is because another application creates this data(video) and dumps it to a folder.
My application has to read it in, and put in an XML tag along with other data pertaining to it.
Any ideas to make this process better from a bandwidth perspective?
|
|
|
|
|
|
I have an app that uses filewatcher to monitor a directory. When a file gets in the directory it reads it, pulss out the jpeg and other data perfectly. Now i would like to display this data on the screen. I have created a picture control and some text controls to dsiplay this data. Anyone have some guidance or good source examples on how to do this?
|
|
|
|
|
You can use a WebBrowser control to show both images and text.
Best regards,
Jaime.
|
|
|
|
|
Any documentation or quick examples on how i would use this control?
I am sort of new to this.
Thanks,
cw
|
|
|
|
|
|
a web browser is not a good fit b/c it shows stuff in a pretty defined way. Just use labels to show text, and pictureboxes to show a picture. Bitmap.FromFile will load the image from that file for you
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi all,
I've created a user-control, which resides in library of other controls I use in my application. The control has two or three textboxes and some drawn graphics. I've put this control in my main application and it basically works fine.
Next I have a class, and I create an instance which stores my data and I serialise this class to file for use later on. The user-control is used to change the values in this object and I do that using an event which is fired when the TextChanged event occurs. What makes it more complicated is I can have multiple instances of the class which I need to switch between and the user-control needs to display the data in the textboxes for the objectI've selected.
My problems see to start when I reload my serialized file or switch between the objects.
So for example, when I initialise the control I do:
usercontrol.textboxA = X.GetParamA;
usercontrol.textboxB = X.GetParamB;
When I switch between say object X and Y I do
usercontrol.textboxA = Y.GetParamA; // This can cause issues as TextChanged is called
usercontrol.textboxB = Y.GetParamB;
When the user changes a value in the textbox I fire the event which is caught in the form containing the user-control I do:
// Get selected object X or Y in this case
Y.GetParamA = usercontrol.textboxA;
Y.GetParamB = usercontrol.textboxB;
This really doesn't feel like the right way to tackle this and I fear my design is off. I though of passing in a reference to my objects or maybe there is a better way to use the events...
Any suggestions would be great.
Thanks
|
|
|
|
|
Looks ok to me, remember most of your work revolves changing data from one format to another and vice versa.
|
|
|
|
|
Hi guys.
How can i execute or fire an event, by example the Validated event for a textbox control from another control like a button?
Example:
I have the following code for the Hello textbox Validated event:
private void Hello_Validated(object sender, EventArgs e)
{
MessageBox.Show("Hello guys");
}
I want to fire that event, the Validated event from another control like a button, so i can show the message "Hello Guys"
by clicking a button.
Thanks for your time...
Regards...
Henry de Sousa
|
|
|
|
|
private void Button_Click(object sender, EventArgs e)
{
Hello_Validated(null, EventArgs.Empty);
}
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|
|
Just call Hello_Validated function from the part of the code you wish.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
Thanks for your answer guys, that's it.
Henry de Sousa
|
|
|
|
|
The following code does NOT give an error but do not know why I do not get the email sent by this code.
Thanks
using System.Net.Mail;
private void mnuFileSendMail_Click(object sender, EventArgs e)
{
try
{
SmtpClient mailClient = new SmtpClient("smtpinternalName.dev");
//Create the mail message
string from = "mywork@company.com";
string to = "myWebaddress@hotmail.com";
string subject = "subject";
string body = "text of the body";
MailMessage mailMessage = new MailMessage(from, to, subject, body);
mailClient.Send(mailMessage); //Send(Mail)
}
catch (Exception ex)
{
throw ex;
}
}
modified on Monday, June 23, 2008 8:55 AM
|
|
|
|
|
please learn to use PRE tags for code blocks!
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
you.suck = (you.passion != Programming)
|
|
|
|