6
Reply

Column named cannot be found?

Resign Designs

Resign Designs

Jun 22 2015 3:04 AM
1.5k
Hi,
 
 I have a datagridview and a delete button.When user select a row and click delete button (outside of the datagridview) it must delete relevant row from datagridview.
 
Right now it is working but sometime it throws this error

"Column named RecordID cannot be found" from below highlighted line

int selectedIndex = InternetGroupDGV.CurrentCell.RowIndex;

if (selectedIndex > -1)
{
int selectedrowindex = InternetGroupDGV.CurrentCell.RowIndex;
DataGridViewRow selectedRow = InternetGroupDGV.Rows[selectedrowindex];
string recID = Convert.ToString(selectedRow.Cells["RecordID"].Value);

SqlConnection con = new SqlConnection(this.configuration.Connstring);
SqlCommand deleteCMD = new SqlCommand("InternetGroup_Delete", con);
deleteCMD.CommandType = CommandType.StoredProcedure;
deleteCMD.Parameters.AddWithValue("@RecordID", recID);
con.Open();
deleteCMD.ExecuteNonQuery();
callDB(); //Bind

}
else
{
MessageBox.Show("Error Occured");
}

Seems me once it deleted a record it cannot find out RecordID to delete again.
Any ideas?
Thanks

Answers (6)