Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
this is my code and i have error at Asteel_total
which one know how can fix it?
thank U
C#
 double Dmid,Dcorner,NoBarA3,NoBarA2,rebNom,Asteel_Total = 0;
 NoBarA3 = 6;
 NoBarA2 = 3;
 double h = double.Parse(txt_B_column.Text, System.Globalization.NumberStyles.Float);
 double Cover = double.Parse(txt_Cover.Text, System.Globalization.NumberStyles.Float);
double b = double.Parse(txt_B_column.Text, System.Globalization.NumberStyles.Float);


 Dmid = double.Parse(txt_BarSize.Text, System.Globalization.NumberStyles.Float);
 Dcorner = double.Parse(txt_Corner_Bar.Text, System.Globalization.NumberStyles.Float);

 DataRow row;
 DataTable Axis2 = GetDt("Axis2");

 dataSet = new DataSet();
 // Add the new DataTable to the DataSet.
 dataSet.Tables.Add(Axis2);
 for (int i = 1; i < NoBarA3; i++)
 {

     row = Axis2.NewRow();
     row["id"] = i;
     row["ds"] = ((h - 2 * Cover) / (NoBarA3 - 1)) * (i - 1) + Cover;
     if (i == 1 || i == (NoBarA2 - 2))
     {
         rebNom = NoBarA2 - 2;
         row["As"] = rebNom * Math.PI * Math.Pow(Dmid, 2) * 0.25 + 2 * Math.PI * Math.Pow(Dcorner, 2) * 0.25;
     }
     else
     {
         row["As"] = 2 * Math.PI * Math.Pow(Dmid, 2) * 0.25;
     }
     row["SteelMaterial_ID"] = cmb_Steel_DSC.SelectedItem;

 }

 DataTable Axis3 = GetDt("Axis3");
 dataSet = new DataSet();
 // Add the new DataTable to the DataSet.
 dataSet.Tables.Add(Axis3);
 for (int i = 1; i < NoBarA2; i++)
 {
     row = Axis3.NewRow();
     row["id"] = i;
     row["ds"] = ((b - 2 * Cover) / (NoBarA2 - 1)) * (i - 1) + Cover;
     if (i == 1 || i == NoBarA2)
     {
         rebNom = NoBarA3 - 2;
         row["As"] = rebNom * Math.PI * Math.Pow(Dmid, 2) * 0.25 + 2 * Math.PI * Math.Pow(Dcorner, 2) * 0.25;
     }
     else
     {
         row["As"] = 2 * Math.PI * Math.Pow(Dmid, 2) * 0.25;
     }
     row["SteelMaterial_ID"] = cmb_Steel_DSC.SelectedItem;
 }

 //Axis3.Rows[1][1].ToString();
 //dgv_DS_C.DataSource = dataSet;
 for (int i = 1; i < NoBarA3; i++)
 {
     Asteel_Total = Asteel_Total + double.Parse(Axis3.Rows[i][1].ToString(), System.Globalization.NumberStyles.Float);

 }


and this is error message
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll

Additional information: There is no row at position 1.
Posted
Updated 17-Oct-14 6:14am
v2
Comments
BillWoodruff 17-Oct-14 12:18pm    
Put break-points in your code: single-step through it (F11) and isolate where/when the error occurs. Then post that information here.
Sergey Alexandrovich Kryukov 17-Oct-14 14:30pm    
In what line?
—SA

1 solution

Why are you explicitly referencing col 1?
C#
Asteel_Total = Asteel_Total + double.Parse(Axis3.Rows[i][1].ToString(), System.Globalization.NumberStyles.Float);
The error would imply that the DataTable has one or zero columns. So use the debugger, and check exactly what Axis3 contains.
 
Share this answer
 
Comments
Darknalk 17-Oct-14 13:40pm    
i see,
axis3.rows.count = 0
but why? i can't understand!!!!
OriginalGriff 17-Oct-14 14:04pm    
Probably, it's because you haven;t added any of your new rows into the Rows collection of Axis3 - DataTable.NewRow does not add the row for you.

http://msdn.microsoft.com/en-us/library/system.data.datatable.newrow(v=vs.110).aspx
Darknalk 17-Oct-14 17:24pm    
thank u so much
OriginalGriff 17-Oct-14 17:31pm    
You're welcome!

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