please delete thread....
-
After 3 hours of frustration I need some help with the findOne() method. FindAll() is no problem but after gong through and trying findOne() code I have only gotten one to work all the others fail (including the one on this site: http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx[^]. The problem seems to be with the search filter, I have to set it to "samaccountname":
DirectorySearcher search = new DirectorySearcher(); //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; search.PropertiesToLoad.Add("memberOf"); StringBuilder groupsList = new StringBuilder(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Append((string)result.Properties["memberOf"][counter]); groupsList.Append("|"); } } else { MessageBox.Show("not found"); }
help!!! shwaguymodified on Friday, December 28, 2007 12:37:58 PM
Does this code compile? What is the error message you get? Did you debug it? What line does it fail on?
Just because we can; does not mean we should.
-
Does this code compile? What is the error message you get? Did you debug it? What line does it fail on?
Just because we can; does not mean we should.
I thought the original posting was pretty clear: -The code works as is, but, when substituting the commented out filters for the working one it fails -The code fails becuase "SearchResult result = search.FindOne();" results in "result = null" -There is no error message, a null value is returned -I am using VS C# Express 2005 and have reinstalled and restarted the PC If there is a better way to trouble-shoot let me know, I need all the help I can get. thanks, shwaguy
-
After 3 hours of frustration I need some help with the findOne() method. FindAll() is no problem but after gong through and trying findOne() code I have only gotten one to work all the others fail (including the one on this site: http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx[^]. The problem seems to be with the search filter, I have to set it to "samaccountname":
DirectorySearcher search = new DirectorySearcher(); //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; search.PropertiesToLoad.Add("memberOf"); StringBuilder groupsList = new StringBuilder(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Append((string)result.Properties["memberOf"][counter]); groupsList.Append("|"); } } else { MessageBox.Show("not found"); }
help!!! shwaguymodified on Friday, December 28, 2007 12:37:58 PM
try this, the only thing I can see different from what I've used.
search.Filter = "(SAMAccountName=" + userName + ")";
only two letters away from being an asset
-
try this, the only thing I can see different from what I've used.
search.Filter = "(SAMAccountName=" + userName + ")";
only two letters away from being an asset
thanks for the reply, but there is nothing wrong with the code as it is posted!!! Using "samaccountname" as the filter criteria works everytime (read the code comments). Using anything else fails, I have tried fully compliant LDAP queries too. I have no issue with the FindAll() method but would like to use this one (I figure that it is faster). help!!!! shwaguy
-
I thought the original posting was pretty clear: -The code works as is, but, when substituting the commented out filters for the working one it fails -The code fails becuase "SearchResult result = search.FindOne();" results in "result = null" -There is no error message, a null value is returned -I am using VS C# Express 2005 and have reinstalled and restarted the PC If there is a better way to trouble-shoot let me know, I need all the help I can get. thanks, shwaguy
I compiled this code:
static void Main(string[] args) { DirectorySearcher search = new DirectorySearcher(); //this filter works string userName = "myusername"; search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; search.PropertiesToLoad.Add("memberOf"); StringBuilder groupsList = new StringBuilder(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Append((string)result.Properties["memberOf"][counter]); groupsList.Append("|"); } } Console.WriteLine(groupsList.ToString()); Console.ReadLine(); }
Resulting in groupList.ToString() = CN=ggotamanda,OU=Groups,OU=OT,OU=CityHall,DC=stpaul,DC=city|CN=ggcbotusers,OU=Groups,OU=CB,OU=CityHall,DC=stpaul,DC=city|CN=ggTMSWebMasters,OU=Groups,OU=IS,OU=CityHall,DC=stpaul,DC=city|CN=ggotusers,OU=Groups,OU=OT,OU=CityHall,DC=stpaul,DC=city| my guess is that the username variable is not known to AD so you get a null result. Beyond that, maybe someone else has an idea.Just because we can; does not mean we should.
-
thanks for the reply, but there is nothing wrong with the code as it is posted!!! Using "samaccountname" as the filter criteria works everytime (read the code comments). Using anything else fails, I have tried fully compliant LDAP queries too. I have no issue with the FindAll() method but would like to use this one (I figure that it is faster). help!!!! shwaguy
shwaguy wrote:
there is nothing wrong with the code as it is posted!!!
Don't go getting you feathers up. If there is nothing wrong, then what is the question? Perhaps you need to calm down and restate the question, more clearly since we are obviously not getting your meaning.
only two letters away from being an asset
-
After 3 hours of frustration I need some help with the findOne() method. FindAll() is no problem but after gong through and trying findOne() code I have only gotten one to work all the others fail (including the one on this site: http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx[^]. The problem seems to be with the search filter, I have to set it to "samaccountname":
DirectorySearcher search = new DirectorySearcher(); //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; search.PropertiesToLoad.Add("memberOf"); StringBuilder groupsList = new StringBuilder(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Append((string)result.Properties["memberOf"][counter]); groupsList.Append("|"); } } else { MessageBox.Show("not found"); }
help!!! shwaguymodified on Friday, December 28, 2007 12:37:58 PM
I did a little more testing for you. The reason the result is null is because AD does not contain an entry for the userName provided. Try using a different username for the userName variable. Confirmed here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/directoryservices_directorysearcher_findone.asp[^] If more than one entry is found during the search, only the first entry is returned. If no entries are found to match the search criteria, a null reference (Nothing in Visual Basic) is returned.
Just because we can; does not mean we should.
-
shwaguy wrote:
there is nothing wrong with the code as it is posted!!!
Don't go getting you feathers up. If there is nothing wrong, then what is the question? Perhaps you need to calm down and restate the question, more clearly since we are obviously not getting your meaning.
only two letters away from being an asset
I seem to get my feathers up when people reply, with the first thought that comes into their head, without actually reading the original post; it tends to destroy a thread. Your post was not so bad, simple mistake...but 2 replies in a row (one not yours) that went down the same path is a bit frustrating. Usually when a thread gets to 2 like this I never get an answer to the question......so I will ask later. thanks shwaguy
-
I seem to get my feathers up when people reply, with the first thought that comes into their head, without actually reading the original post; it tends to destroy a thread. Your post was not so bad, simple mistake...but 2 replies in a row (one not yours) that went down the same path is a bit frustrating. Usually when a thread gets to 2 like this I never get an answer to the question......so I will ask later. thanks shwaguy
shwaguy wrote:
so I will ask later.
I'll remember to ignore you then.
only two letters away from being an asset
-
I did a little more testing for you. The reason the result is null is because AD does not contain an entry for the userName provided. Try using a different username for the userName variable. Confirmed here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/directoryservices_directorysearcher_findone.asp[^] If more than one entry is found during the search, only the first entry is returned. If no entries are found to match the search criteria, a null reference (Nothing in Visual Basic) is returned.
Just because we can; does not mean we should.
-
After 3 hours of frustration I need some help with the findOne() method. FindAll() is no problem but after gong through and trying findOne() code I have only gotten one to work all the others fail (including the one on this site: http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx[^]. The problem seems to be with the search filter, I have to set it to "samaccountname":
DirectorySearcher search = new DirectorySearcher(); //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; search.PropertiesToLoad.Add("memberOf"); StringBuilder groupsList = new StringBuilder(); SearchResult result = search.FindOne(); if (result != null) { int groupCount = result.Properties["memberOf"].Count; for (int counter = 0; counter < groupCount; counter++) { groupsList.Append((string)result.Properties["memberOf"][counter]); groupsList.Append("|"); } } else { MessageBox.Show("not found"); }
help!!! shwaguymodified on Friday, December 28, 2007 12:37:58 PM
It is proper to keep a thread even if you already found the solution. Someone later on might have the same type of problem and this thread could help them.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
-
It is proper to keep a thread even if you already found the solution. Someone later on might have the same type of problem and this thread could help them.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
Hi Paul, I agree. In this case no solution was found for the original question and the thread turned into an unreadable mess that would only serve to waste the time of someone seeking a solution. Usually, I get good or great responses from this forum. People usually read, think then answer (even if the correct solution is not given). Maybe I just asked on a bad day. shwaguy
-
Hi Paul, I agree. In this case no solution was found for the original question and the thread turned into an unreadable mess that would only serve to waste the time of someone seeking a solution. Usually, I get good or great responses from this forum. People usually read, think then answer (even if the correct solution is not given). Maybe I just asked on a bad day. shwaguy
shwaguy wrote:
Maybe I just asked on a bad day.
Or, more likely, you asked a poorly defined question and are now upset and pouting. :((
only two letters away from being an asset
-
shwaguy wrote:
so I will ask later.
I'll remember to ignore you then.
only two letters away from being an asset
thanks for letting me know you will ignore my threads...it was very useful on topic information (sc). I understand that people make mistakes, but, when I get 3 knee jerk responses (responses that have NO input to the question) in a row I tend to figure noone will want to read the thread. I twice stated that there was no issue with the existing code, but, you seemed to miss that. Your ego must be easily bruised if to be hurt by those exclamation marks. -If a response is not the solution thats all good. -If a response is correct solution thats all good. -If the response is off topic becuase the poster did not bother to read the post....I think "I wish that the poster could delete the response, its a waste of reading time and clutters the original post" You take offense way too easily. Just because you have 3500 posts does not mean I am kissing your butt for very poor, of topic, replies. I don't have anything against you either, I think you may have helped a lot of threads. shwaguy
-
shwaguy wrote:
Maybe I just asked on a bad day.
Or, more likely, you asked a poorly defined question and are now upset and pouting. :((
only two letters away from being an asset
-
Hi Paul, I agree. In this case no solution was found for the original question and the thread turned into an unreadable mess that would only serve to waste the time of someone seeking a solution. Usually, I get good or great responses from this forum. People usually read, think then answer (even if the correct solution is not given). Maybe I just asked on a bad day. shwaguy
shwaguy wrote:
the original question
It did sound rather vague.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
thanks for letting me know you will ignore my threads...it was very useful on topic information (sc). I understand that people make mistakes, but, when I get 3 knee jerk responses (responses that have NO input to the question) in a row I tend to figure noone will want to read the thread. I twice stated that there was no issue with the existing code, but, you seemed to miss that. Your ego must be easily bruised if to be hurt by those exclamation marks. -If a response is not the solution thats all good. -If a response is correct solution thats all good. -If the response is off topic becuase the poster did not bother to read the post....I think "I wish that the poster could delete the response, its a waste of reading time and clutters the original post" You take offense way too easily. Just because you have 3500 posts does not mean I am kissing your butt for very poor, of topic, replies. I don't have anything against you either, I think you may have helped a lot of threads. shwaguy
:laugh: :laugh: :laugh:
shwaguy wrote:
I twice stated that there was no issue with the existing code,
Then why did you post it? Go away, son, you bore me.
only two letters away from being an asset
-
shwaguy wrote:
the original question
It did sound rather vague.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
Putting aside the 2 replies I got being totally off topic: - "what error message did you get?" - uh...none...I got a null value as stated. - replace code that was stated as working search.Filter = "samaccountname=" + userName; with this other filter that works equally well too (blah, blah, blah). Whats vague about the post?: //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; SearchResult result = search.FindOne(); shwaguy
-
:laugh: :laugh: :laugh:
shwaguy wrote:
I twice stated that there was no issue with the existing code,
Then why did you post it? Go away, son, you bore me.
only two letters away from being an asset
You just have to feel like your right on this don't you? Here is the news, you are wrong again. The code although correct did not suit my purpose. If you bothered to read the posting you would have noted that I commented out (with comments) the items that would not work, but wanted to work. as previously stated: beat it.
-
Putting aside the 2 replies I got being totally off topic: - "what error message did you get?" - uh...none...I got a null value as stated. - replace code that was stated as working search.Filter = "samaccountname=" + userName; with this other filter that works equally well too (blah, blah, blah). Whats vague about the post?: //this filter works search.Filter = "samaccountname=" + userName; //This below formats for the search filter result in result=null //search.Filter = String.Format("(cn={0})", userName); //code project filter //search.Filter = "cn=" + userName; SearchResult result = search.FindOne(); shwaguy
The original post was rather vague. Not the ones that followed.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon