0

I have a foreach loop run for every record in my list. When I execute it the it reaches the ExecuteNonQuery() and it throws this error:

String or binary data would be truncated. The statement has been terminated.

I have checked to make sure the field can hold my values an it can aka varchar(10) for L_Number Now the S_O_Number is just int. Could this be the issue?

Here is my method that I run that is causing the truncation error.

 foreach (DataGridViewRow dr in dropdeadGridView.Rows)
 {
     bool checkBoxValue = Convert.ToBoolean(dr.Cells[5].Value);
     if (checkBoxValue)
     {
         o.Add(dr.Cells[1].Value.ToString());
     }
 }

 foreach (string num in o)
 {
     SqlConnection dbConn = DBHelper.getConnection();
     try
     {
         using (dbConn)
         {
             SqlCommand addJob = new SqlCommand(@"UPDATE OrderDetail SET L_NUMBER = @L_NUMBER WHERE S_O_NUMBER = @SONumber", dbConn);
             addJob.Parameters.AddWithValue("@SONumber", num);
             addJob.Parameters.AddWithValue("@L_NUMBER", lNumber.Text);
             dbConn.Open();
             addJob.ExecuteNonQuery();//error thrown
         }
         MessageBox.Show("Updated!");
     }
     catch (Exception ex)
     {

throw ex;
     }

What happens above is it gets the values from my gridview based of checkbox selection. Puts them into a list. Which then I run a foreach loop to run for every value in the list. Why am I getting a truncation error when the data fits the size of the field?

0 Answers0