Hi,
I have an event written when the user clicks on a row in a datagridview. I want to refer to the value of the first cell in the row in a parameterized query and use that to place in another datagridview. I have never done this before, so I'm not
sure of the syntax to place the selected cell's content in the parameterized query .addwithvalue statement.
Here's some code:
private void gridCall_CellContentClick(object sender, DataGridViewCellEventArgs e) { // string str; // str = gridCall.Rows[gridCall.SelectedRows[0].Index].Cells[1].Value.ToString(); // string str = gridCall.SelectedRows[0].Cells[0].Value.ToString(); // string str = gridCall.CurrentCell; con = new SqlConnection("server=###;database=###;Initial Catalog=###;uid=###;pwd=###"); queryString = "select a.note from cadnote a inner join callhist b on a.incidentno = b.incidentno where a.incidentno = @incidentno"; DataTable table = new DataTable("DVNotesTable"); using (SqlConnection myConnection = con) { using (SqlCommand sqlComm = new SqlCommand(queryString, con)) { sqlComm.Parameters.AddWithValue("@incidentno", ?????); sqlComm.CommandType = CommandType.Text; using (SqlDataAdapter da = new SqlDataAdapter(sqlComm)) { try { myConnection.Open(); da.Fill(table); gridNotes.DataSource = table; } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { myConnection.Close(); } } } } }
As you can see, at the beginning of the click event I have tried a few things but when I plug them into the parameterized query I get an error "index out of range". How do I refer to the selected cell value in the parameterized query .addwithvalue line (where I have just put ???? for the time being)?
Greatly appreciate your help. Thank you.