JoanHerrick
asked on
DataRow substring works fine unless there is a null value.
I am trying to convert a datarow value to a substring and it works fine unless the value of the datarow is null. I've tried several if statements and none seem to be working.
Here's my code to the substring
foreach(DataRow dm in dt.Rows)
{
dm["mobile"]=dm["mobile"]. ToString() .Substring (4);
}
My datatableresults display in a datagrid which will always have some of these fields with data and some without.
if there isn't any value in the row I get this error
Length cannot be less than zero. Parameter name: length
Any ideas?
Here's my code to the substring
foreach(DataRow dm in dt.Rows)
{
dm["mobile"]=dm["mobile"].
}
My datatableresults display in a datagrid which will always have some of these fields with data and some without.
if there isn't any value in the row I get this error
Length cannot be less than zero. Parameter name: length
Any ideas?
ASKER
Doesn't work. Same error. Here's more of my code in case that helps. Oh, thanks for your speedy response!
public DataSet FindUsers(string sFilter, string[] columns, string path, bool useCached)
{
HttpContext context = HttpContext.Current;
DataSet userDS = (DataSet)context.Cache[sFi lter];
if((userDS == null) || (!useCached))
{
DirectoryEntry deParent = new DirectoryEntry("ad server string");
deParent.Username = @"username";
deParent.Password = "password";
deParent.AuthenticationTyp e = AuthenticationTypes.Secure ;
DirectorySearcher ds = new DirectorySearcher(
deParent,
sFilter,
columns,
SearchScope.Subtree
);
ds.PageSize = 1000;
using(deParent)
{
userDS = new DataSet("userDS");
DataTable dt = userDS.Tables.Add("users") ;
DataRow dr;
foreach(string prop in columns)
{
dt.Columns.Add(prop, typeof(string));
}
using (SearchResultCollection src = ds.FindAll())
{
foreach(SearchResult sr in src)
{
dr = dt.NewRow();
foreach(string prop in columns)
{
if(sr.Properties.Contains( prop))
{
dr[prop] = sr.Properties[prop][0].ToS tring();
}
}
dt.Rows.Add(dr);
}
foreach (DataRow dm in dt.Rows)
{
if(dm["mobile"]!=null)
{
dm["mobile"]=dm["mobile"]. ToString() .Substring (4);
}
}
}
}
context.Cache.Insert(sFilt er, userDS, null, DateTime.MaxValue, TimeSpan.FromSeconds(180)) ;
}
return userDS;
}
private void Page_Load(object sender, System.EventArgs e)
{
TextBox1.Attributes.Add("o nkeydown", "KeyDownHandler"+base.Port letID.ToSt ring()+"(' Button1')" );
}
private void Button1_Click(object sender, System.EventArgs e)
{
string qry = String.Format("(&(objectCa tegory=per son)(|(giv enName={0} *)(sn={0}* )))", TextBox1.Text);
string[] columns = new string[]{"givenName", "sn", "cn", "department", "telephoneNumber", "mobile"};
string ldapPath = "ad server string";
DataSet ds = FindUsers(qry, columns, ldapPath, true);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
}
public DataSet FindUsers(string sFilter, string[] columns, string path, bool useCached)
{
HttpContext context = HttpContext.Current;
DataSet userDS = (DataSet)context.Cache[sFi
if((userDS == null) || (!useCached))
{
DirectoryEntry deParent = new DirectoryEntry("ad server string");
deParent.Username = @"username";
deParent.Password = "password";
deParent.AuthenticationTyp
DirectorySearcher ds = new DirectorySearcher(
deParent,
sFilter,
columns,
SearchScope.Subtree
);
ds.PageSize = 1000;
using(deParent)
{
userDS = new DataSet("userDS");
DataTable dt = userDS.Tables.Add("users")
DataRow dr;
foreach(string prop in columns)
{
dt.Columns.Add(prop, typeof(string));
}
using (SearchResultCollection src = ds.FindAll())
{
foreach(SearchResult sr in src)
{
dr = dt.NewRow();
foreach(string prop in columns)
{
if(sr.Properties.Contains(
{
dr[prop] = sr.Properties[prop][0].ToS
}
}
dt.Rows.Add(dr);
}
foreach (DataRow dm in dt.Rows)
{
if(dm["mobile"]!=null)
{
dm["mobile"]=dm["mobile"].
}
}
}
}
context.Cache.Insert(sFilt
}
return userDS;
}
private void Page_Load(object sender, System.EventArgs e)
{
TextBox1.Attributes.Add("o
}
private void Button1_Click(object sender, System.EventArgs e)
{
string qry = String.Format("(&(objectCa
string[] columns = new string[]{"givenName", "sn", "cn", "department", "telephoneNumber", "mobile"};
string ldapPath = "ad server string";
DataSet ds = FindUsers(qry, columns, ldapPath, true);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
}
what is the exact error message and in which line is the exception thrown?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i would recommend not using the try catch block solution without any exception evaluation.
the rest is fine ;)
the rest is fine ;)
Valid point, thanks! Didn't catch that :P... should have just copy and pasted something in :)
ASKER
Thanks experts - can you explain about the exception evaluation?
The try / catch blow above just assumes that an occurring exception was because of a null.
If there were memory problems, dataset issues or other malfunctions, in the code snippet above, none of them are
accounted / checked for.
If there were memory problems, dataset issues or other malfunctions, in the code snippet above, none of them are
accounted / checked for.
{
if(dm["mobile"] != null)
{
dm["mobile"]=dm["mobile"].
}
}