Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my class file am trying to achieve something like this

If i have a string

Eg: Joe (Space) will.Smith then

i will split the string based on dot like

First Name: Joe (space)will
Last Name:Smith

Then remove the space from first name(it will be "Joewill") then pass the firstname and last name to search filter to retrieve user info from AD.

So far i got this,but it throwing error.

but am getting this error :String cannot be of zero length.
C#
if (username.Contains("."))
           {
               string Fname = username.Substring(0, username.IndexOf("."));
               Fname = Fname.Replace("", " ");
               string[] splitString = username.Split(new char[] { '.' });
               username = splitString[splitString.Length - 1];

           }

 if (username.IndexOf(" ") > 0)
                   search.Filter = "(cn=" + username + ")";
               else
                   search.Filter = "(sn=" + username + ")" & "(givenname=" + Fname + "));
Posted
Updated 23-Feb-11 2:00am
v2
Comments
senguptaamlan 23-Feb-11 7:55am    
try to debug the splitString function that you have written..
shan1395 23-Feb-11 8:09am    
i was testing this code with out replace ,it works fine,"mkgoud" corrected my mistake

so the whole code is suppose to be like this

if (username.Contains("."))
{
string Fname = username.Substring(0, username.IndexOf("."));
Fname = Fname.Replace(" ","");
string[] splitString = username.Split(new char[] { '.' });
username = splitString[splitString.Length - 1];

}

if (username.IndexOf(" ") > 0)
search.Filter = "(cn=" + username + ")";
else
search.Filter = "(sn=" + username + ")" & "(givenname=" + Fname + "));

Change this
Fname = Fname.Replace(" ","");


[EDIT]

C#
if (username.IndexOf(" ") > 0)
                    search.Filter = "(cn=" + username + ")";
                else
                    search.Filter = "(sn=" + username + ")(givenname=" + Fname + ")";

If you also want & then
"(sn=" + username + ")&(givenname=" + Fname + ")";


[/EDIT]
 
Share this answer
 
v2
Comments
shan1395 23-Feb-11 8:04am    
thanks a lot for your response,is this my search filter condition is correct ?

search.Filter = "(sn=" + username + ")" & "(givenname=" + Fname + ")); (some how i have doubt in this line too)
m@dhu 23-Feb-11 8:15am    
Check my updated answer.
shan1395 23-Feb-11 16:54pm    
Thanks mkgoud again you shown me the right path.

I have problem in my search filter its not accepting "Fname",it throws error

"The name (fname) doesn't exist in the current context"

fjdiewornncalwe 23-Feb-11 9:00am    
+5. Good catch. That argument reversal in the replace jumped out at me right away as well.
m@dhu 23-Feb-11 23:54pm    
Thanks sir.
I'd probably do this if I knew for sure the string would always be First.Last names, with or without spaces.

C#
username = username.Replace(" ", "");
string[] FirstLast = username.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
search.Filter = "(sn=" + FirstLast[1] + ") & (givenname=" + FirstLast[0] + ")";


Not much different...just a different order, mainly.

BUT, if the string would always be First.Last (with or without spaces), and the common name format is First.Last, why go through all the trouble?

username = username.Replace(" ","");
search.Filter = "(cn=" + username + ")";


D'oh! Filter syntax is wrong...sorry. For multiple criteria, do this:
filter = "(&(sn="+FirstLast[1]+")(givenName=" + FirstLast[0] + "))";


or, from your orginal:

search.Filter = "(&(sn=" + username + ")(givenname=" + Fname + "));
 
Share this answer
 
v3
Comments
shan1395 23-Feb-11 16:47pm    
If you seen my code,am handling three different (actually 4) scenario.

parameter always pass like

Joe will.smith
Joewill.smith
joe will smith

that why i used if condition in my filter.if string comes with "dot" then i will split the string (first name and last name) and compare against AD to retrieve the user.
GenJerDan 23-Feb-11 17:04pm    
OK.
Working for you now?
If not, and the code above is what you still have, "FName" is only getting a value if the original string contains a "."
shan1395 23-Feb-11 17:40pm    
Hi GenJerDan,

mkgoud code is retrieve the value for Fname but when i pass it to my search filter ,throws error like

"The name (fname) doesn't exist in the current context"

am not sure why its not recoganize the fname.
shan1395 23-Feb-11 18:00pm    
Ignore the previous comment,i remodified the code like below.

if (username.Contains("."))
{
string Fname = username.Substring(0, username.IndexOf("."));
Fname = Fname.Replace(" ", "");

string[] splitString = username.Split(new char[] { '.' });
username = splitString[splitString.Length - 1];

search.Filter = "(sn=" + username + ")&(givenname=" + Fname + ")";//Throwing Exception

//This statement pass the value
search.Filter = "(sn=" + username + ")";
}


if (username.IndexOf(" ") > 0)

search.Filter = "(cn=" + username + ")";




If i user firstname and last name in search filter,its throwing exception.but if i use only first name or last name,then it returns output.

Am not sure the exact syntax for search filter with conditions
GenJerDan 24-Feb-11 9:27am    
Yeah, I messed up the syntax, too. Correct syntax put onto the bottom of the Aanswer. Basically, move the "&" to the beginning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900