0

When I try to update the selected row I get a error:

'Index was outside the bounds of the array.'

where I declare the DataRow dr = .... Is this the correct way of doing it? the ind value updates as i click on other rows in the data grid.

EDIT

So I found the problem, the "index" is using the serial numbers in the DB, which are like this 6066123400654.. so obviously that is why I got the error. I changed the serial number to 0,1,2,3,4 respectively and now I can update fine, but the question now is how do I use the real serial numbers as the index?? I'm really confused now.

public bool UpdateComponents(List<int> index, DataTable Data)
{

    foreach (int ind in index)
    {
        DataRow dr = Data.Select("SerialNumber ="+ ind.ToString())[0];
        {
            using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["SHS_CON"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("usp_UpdateComponent", sqlcon))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@serialnumber", dr[0]);
                    cmd.Parameters.Add("@manufacturer", dr[1]);
                    cmd.Parameters.Add("@model", dr[2]);
                    cmd.Parameters.Add("@description", dr[3]);
                    cmd.Parameters.Add("@price", dr[4]);


                    sqlcon.Open();

                    cmd.ExecuteNonQuery();

                    sqlcon.Close();

                }
            }
        }
    }
    return true;
}
  • 4
    well... was there a row with that serial number? Since you haven't actually got past the `dr = ...` line, I doubt that this is anything to do with the stored proc – Marc Gravell May 23 '18 at 14:13
  • Exactly as @MarcGravell said, maybe first do the `Data.Select`, then add in an `if` statement to ensure rows exist and, then assign the `dr` variable. – John Bustos May 23 '18 at 14:16
  • ok I found the issue.. the serial numbers were like 5465756, so obviously it's not going to work. So I've change the dummy data serial numbers to 1, 2 ,3 , 4 respectively and the the update worked fine. So my question now is how do I use the real serial numbers as the "index". – Carl Cronje May 23 '18 at 14:33

0 Answers0