911...Need Help! : Length cannot be less than zero. Parameter name : length

M

manmit.walia

Hello All and Thank You for your time,
I am stuck on this exception handleing error. The problem is that when
I run my application the application works perfect but sometimes, I get
this error. I do not know where it is causing it but my guess would be
something with the function below. Any help would be greatfull, as this
would be a learning experience for me.

For this example: swTitle.Text = "AutoCAD 2004"
After the user selects the product from a listbox, a datagrid is binded
to an arraylist which is field with all of the <EmployeeName>
associated with that product.
So I have split the EmployeeName up into four fields (FirstName, MI,
LastName, Email). and each field will be a column in the datagrid.


Function Task:
**********************
1) Read XML file
2) Use XPath to find a particular node
3) Query the node for fields and print out the fields in datagrid


XML File Example:
**********************
<EMPProductType>
<ProductType>AutoCAD 2004</ProductType>
<ManagerEMail>[email protected]</ManagerEMail>
<EmployeeName>Joe,,Alexander,[email protected]</EmployeeName>
<EmployeeName>Joe,,Allen,[email protected]</EmployeeName>
<EmployeeName>Harold,,Anderson,[email protected]</EmployeeName>

<EmployeeName>Kent,,Berner,[email protected]</EmployeeName>
<EmployeeName>Tommy,,Childers,[email protected]</EmployeeName>
</EMPProductType>

Function:
**********************
void GetUserList()
{

XmlDocument doc = new XmlDocument();
doc.Load("http://dpi948.delta.com/dtedmprod:/Field_Operations/ETAM/SFT/Software.xml");

//XmlNode sw;
XmlNodeList sw;
XmlNode root = doc.DocumentElement;

// SEARCH XML FILE
string t = "'" + swTitle.Text + "'";
sw=root.SelectNodes("//EMPProductType[ProductType=" + t + "
]/EmployeeName");
total = sw.Count;
// RETURN DATA FROM XPATH
if(sw != null)
{

for (int i=0; i < sw.Count; i++)
{
len = sw.InnerXml.Length;
ind = sw.InnerXml.IndexOf(',');
fn = sw.InnerXml.Substring(0, ind);
ind = ind + 1;
len = len - ind;
s1 = sw.InnerXml.Substring(ind,len);
ind = s1.IndexOf(',');
mi = s1.Substring(0, ind);
if(mi == "")
{
mi = "x";
}
ind = ind + 1;
len = len - ind;
s1 = s1.Substring(ind, len);
ind = s1.IndexOf(',');
ln = s1.Substring(0, ind);
ind = ind + 1;
len = len - ind;
ue = s1.Substring(ind, len);

ul.Add (new user(fn, mi, ln, ue));


temp = temp + " " + fn + " " + mi + " " + ln + " " + ue + "<br>";

}
usedCount.Text = total.ToString();
myDataGrid.DataSource=ul;
myDataGrid.DataBind();
}

}
 
G

Guest

Just a guess, but it is most likely the fact that most of your people have no
middle name. From the error, you are probably erroring out trying to submit
to a database. If so, you will have to set the default for that parameter to
null and not set it when the string is empty.

if(txtMiddleName.Text.ToString().Length >= 1)
{
//Set Parameter
}
else
{
//Do nothing
}

You do not need the else, but it illustrates the point.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


Hello All and Thank You for your time,
I am stuck on this exception handleing error. The problem is that when
I run my application the application works perfect but sometimes, I get
this error. I do not know where it is causing it but my guess would be
something with the function below. Any help would be greatfull, as this
would be a learning experience for me.

For this example: swTitle.Text = "AutoCAD 2004"
After the user selects the product from a listbox, a datagrid is binded
to an arraylist which is field with all of the <EmployeeName>
associated with that product.
So I have split the EmployeeName up into four fields (FirstName, MI,
LastName, Email). and each field will be a column in the datagrid.


Function Task:
**********************
1) Read XML file
2) Use XPath to find a particular node
3) Query the node for fields and print out the fields in datagrid


XML File Example:
**********************
<EMPProductType>
<ProductType>AutoCAD 2004</ProductType>
<ManagerEMail>[email protected]</ManagerEMail>
<EmployeeName>Joe,,Alexander,[email protected]</EmployeeName>
<EmployeeName>Joe,,Allen,[email protected]</EmployeeName>
<EmployeeName>Harold,,Anderson,[email protected]</EmployeeName>

<EmployeeName>Kent,,Berner,[email protected]</EmployeeName>
<EmployeeName>Tommy,,Childers,[email protected]</EmployeeName>
</EMPProductType>

Function:
**********************
void GetUserList()
{

XmlDocument doc = new XmlDocument();
doc.Load("http://dpi948.delta.com/dtedmprod:/Field_Operations/ETAM/SFT/Software.xml");

//XmlNode sw;
XmlNodeList sw;
XmlNode root = doc.DocumentElement;

// SEARCH XML FILE
string t = "'" + swTitle.Text + "'";
sw=root.SelectNodes("//EMPProductType[ProductType=" + t + "
]/EmployeeName");
total = sw.Count;
// RETURN DATA FROM XPATH
if(sw != null)
{

for (int i=0; i < sw.Count; i++)
{
len = sw.InnerXml.Length;
ind = sw.InnerXml.IndexOf(',');
fn = sw.InnerXml.Substring(0, ind);
ind = ind + 1;
len = len - ind;
s1 = sw.InnerXml.Substring(ind,len);
ind = s1.IndexOf(',');
mi = s1.Substring(0, ind);
if(mi == "")
{
mi = "x";
}
ind = ind + 1;
len = len - ind;
s1 = s1.Substring(ind, len);
ind = s1.IndexOf(',');
ln = s1.Substring(0, ind);
ind = ind + 1;
len = len - ind;
ue = s1.Substring(ind, len);

ul.Add (new user(fn, mi, ln, ue));


temp = temp + " " + fn + " " + mi + " " + ln + " " + ue + "<br>";

}
usedCount.Text = total.ToString();
myDataGrid.DataSource=ul;
myDataGrid.DataBind();
}

}
 
M

manmit.walia

Thanks Cowboy,
But am I not doing that here:

mi = s1.Substring(0, ind);
if(mi == "")
{
mi = "x";

}
 

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top