lye nian

lye nian

  • NA
  • 7
  • 8k

How to fix “There is no row at position 1”

Apr 24 2019 10:18 PM
I arranging and extracting data from database to the datagrid, i am trying to custom the column and rows to display the data desired, through the looping i tried to match the data from the one of the datatable to get another data to fill into the datagrid, unfortunately i keep getting "There is no row at position 1 error". I've attached attachment to illustrate the problems faced by me.
 
 
 

I've gone through some of the posts in stackoverflow to resolve the problems,i can't find the problems. 1. Error There is no row at position 1 2. there is no row at position 1

  1. private void generate_view1(DataTable search_data1)  
  2. {  
  3.     DataTable sample_data1 = new DataTable();  
  4.     sample_data1 = search_data1.Copy();  
  5.     sample_data1.Columns.Add("Item_Number");  
  6.     sample_data1.Columns.Add("Min_Tolerance");  
  7.     sample_data1.Columns.Add("Max_Tolerance");  
  8.   
  9.   
  10.     var database_select = new DatabaseSelect();  
  11.     string OrderNo = Order_NO.Text.ToString();  
  12.     DataTable result_data = new DataTable();  
  13.     result_data = database_select.LoadTable_DOHEAD(OrderNo);  
  14.   
  15.     string SalesNO = result_data.Rows[0]["EAORNO"].ToString();  
  16.     string Del_index = result_data.Rows[0]["EADLIX"].ToString();  
  17.   
  18.     var database_select1 = new DatabaseSelect();  
  19.     DataTable result_weight = new DataTable();  
  20.     result_weight = database_select.LoadTable_MITTRA(SalesNO, Del_index);  
  21.   
  22.   
  23.     dataGrid1.ItemsSource = null;  
  24.     dataGrid1.ItemsSource = sample_data1.DefaultView;  
  25.     DataGridTextColumn Weight1 = new DataGridTextColumn();  
  26.     dataGrid1.DisplayMemberPath = sample_data1.Columns["MTTRQT"].ToString();  
  27.   
  28.     Decimal sum = Convert.ToDecimal(sample_data1.Compute("SUM(MTTRQT)"string.Empty));  
  29.   
  30.     Weight.Text = (sum * -1).ToString();  
  31.      //******Problems occur in below this part*********//  
  32.     for (int i = 0; i < sample_data1.Rows.Count; i++)  
  33.     {  
  34.         string itemcode = sample_data1.Rows[i]["MTITNO"].ToString();  
  35.         String ItemNumber = result_weight.Rows[i]["MTITNO"].ToString();  
  36.   
  37.         if (itemcode != " ")  
  38.         {  
  39.             sample_data1.Rows[i]["Item_Number"] = result_weight.Rows[i]["MTITNO"].ToString();  
  40.   
  41.             var database_select2 = new DatabaseSelect();  
  42.   
  43.             DataTable result_tolerance = new DataTable();  
  44.             result_tolerance = database_select.LoadUser_Tolerance(ItemNumber); //** i've include my loadUser Tolerance query in here as well***//  
  45.             sample_data1.Rows[i]["Min_Tolerance"] = result_tolerance.Rows[i]["Min_Tol"].ToString();     //*** this is the line where i face the problems***//  
  46.             string MINTOL = result_tolerance.Rows[i]["Min_Tol"].ToString();    //*** i've put in this to illustrate the problems**//  
  47.             MessageBox.Show(MINTOL);    //*** i've put in this to illustrate the problems**//  
  48.   
  49.         }  
  50.   
  51.     }
LoadUser_Tolerance variable query( i am trying to select min tolerance from this query to insert it into my datagrid) For the results desired( you can refer the illustrations above), i only having this issues whenever i tried to get MinTolerance to display in my DataGrid. i can't figure out how to make this right.

  1. public DataTable LoadUser_Tolerance(string ItemNO)  
  2. {  
  3.     OdbcConnection conn = new OdbcConnection();  
  4.     conn.ConnectionString = ConString;  
  5.     conn.Open();  
  6.     CmdString = "SELECT Item_NO, Min_Tol, Max_Tol FROM MasTolerance where Item_NO ='" + ItemNO + "'";  
  7.     OdbcCommand cmd = new OdbcCommand(CmdString, conn);  
  8.     OdbcDataAdapter sda = new OdbcDataAdapter(cmd);  
  9.     DataTable dt = new DataTable("MasTolerance");  
  10.     sda.Fill(dt);  
  11.     return dt;  

Debugging Attempts Tried.
  1. if (result_tolerance.Rows.Count > i)  
  2. {  
  3.   sample_data1.Rows[i]["Min_Tolerance"] = result_tolerance.Rows[i]["Min_Tol"].ToString();  
  4.   string MINTOL = result_tolerance.Rows[i]["Min_Tol"].ToString();  
  5.   MessageBox.Show(MINTOL + i);  
  6. }  
  7. else  
  8. {  
  9.     MessageBox.Show("Error"); //** i can display the Item number in row 1, but Min tolerance for the Row 1 can't be display**//  
  10.   

Sorry to make the whole thing this long, but I have no choice.

Answers (5)