asp.net - Search Active directory and return null or "" into a label in c# if property is blank -


i trying search active directory users details it. populate labels details below. works fine if user doesn't have value 'division' bombs out below error message. have tried different things cant work show null or "" in label text help!

   private void populate_table(string current_user)     {         string connection = configurationmanager.connectionstrings["adconnection"].tostring();          directorysearcher dssearch = new directorysearcher(connection);         dssearch.filter = "(samaccountname=" + current_user + ")";         searchresult sresult = dssearch.findone();         directoryentry dsresult = sresult.getdirectoryentry();          lblfname.text = dsresult.properties["givenname"][0].tostring();         lbllname.text = dsresult.properties["sn"][0].tostring();         lblemail.text = dsresult.properties["mail"][0].tostring();         lbldepartment.text = dsresult.properties["department"][0].tostring();         lblsam.text = dsresult.properties["samaccountname"][0].tostring();         lblbranch.text = dsresult.properties["division"][0].tostring();     } 

error

index out of range. must non-negative , less size of collection.

you need check see if given property set or not:

if (dsresult.properties["division"] != null &&     dsresult.properties["division"].count > 0) {      lblbranch.text = dsresult.properties["division"][0].tostring(); } else      {      lblbranch.text = string.empty; } 

this way ad works - need checking any property not required property. non-nullable "not set" in ad , .properties["...."] null


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -