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! :)