0

I'm trying to update multiple rows in a data grid, the code gets the job done but I still seem to get a

Object reference not set to an instance of an object

When I check the records the desired status updates accordingly for the selected records within the gridview.

private void UpdateWorkerStatus()
{
    SqlCommand cmdUpdate = new SqlCommand(@"UPDATE Workers2
                                       SET WorkerStatus = @WorkerStatus
                                       WHERE FullName = @FullName", cn);

    cmdUpdate.Parameters.AddWithValue("@WorkerStatus", SqlDbType.VarChar).Value = txtWorkerStatus.Text;
    cmdUpdate.Parameters.AddWithValue("@FullName", SqlDbType.VarChar).Value = txtFullName.Text;

    foreach (DataGridViewRow row in grdWorkers.Rows)
    {
        cmdUpdate.Parameters["@WorkerStatus"].Value = row.Cells["WorkerStatus"].Value.ToString();
        cmdUpdate.Parameters["@FullName"].Value = row.Cells["FullName"].Value.ToString();
        cmdUpdate.ExecuteNonQuery();
    }
}

thank you in advance! :)

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Ron Reyes
  • 35
  • 1
  • 6

3 Answers3

1
private void UpdateWorkerStatus()
{
    SqlCommand cmdUpdate = new SqlCommand(@"UPDATE Workers2
                                   SET WorkerStatus = @WorkerStatus
                                   WHERE FullName = @FullName", cn);

    cmdUpdate.Parameters.AddWithValue("@WorkerStatus", SqlDbType.VarChar).Value = txtWorkerStatus.Text;
    cmdUpdate.Parameters.AddWithValue("@FullName", SqlDbType.VarChar).Value = txtFullName.Text;

    foreach (DataGridViewRow row in grdWorkers.Rows)
    {
        cmdUpdate.Parameters["@WorkerStatus"].Value = row.Cells["WorkerStatus"].Value!=DBNull.Value? row.Cells["WorkerStatus"].Value.ToString():"";
        cmdUpdate.Parameters["@FullName"].Value = row.Cells["FullName"].Value!= DBNull.Value ? row.Cells["FullName"].Value.ToString():"";
        cmdUpdate.ExecuteNonQuery();
    }
}
0

Try this,Instead of .ToString() use Convert.Tostring( row.Cells["FullName"].Value) Same for "WorkStatus"

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Akshay Belure
  • 204
  • 3
  • 14
0

Since you don't supply us with the information of where the exception is thrown, I'm going to assume it's on one of the .ToString() calls as those are the most likely. Probably one of the values for WorkerStatus or FullName are null, resulting in the exception when you call the .ToString() method.

I would check the values for null before calling the .ToString(). This enables you to fill the parameter values with something meaningful if the value you are trying to read is null:

if (row.Cells["WorkerStatus"].Value != null)
    cmdUpdate.Parameters["@WorkerStatus"].Value = row.Cells["WorkerStatus"].Value.ToString();
else
    cmdUpdate.Parameters["@WorkerStatus"].Value = string.Empty // or some meaningful default value of your chosing

You can reduce this statement to one line using this:

cmdUpdate.Parameters["@WorkerStatus"].Value = (row.Cells["WorkerStatus"].Value != null) ? row.Cells["WorkerStatus"].Value.ToString() : string.Empty;

And of course you should do the same for FullName.

elloco999
  • 80
  • 7