I made a database and a program with a datagridview to show the database content. Now I want to make a button to let users delete a row and delete it also in the database.
I tried the following solution which I found in Stackoverflow: How to delete row datagrid..
The error tells me that the type SqlConnection was not found.
My whole code I've linked here:Pastebin
private void button5_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
return;
string sql = "DELETE FROM ticket.support WHERE ID = @rowID";
using (SqlConnection myConnection = new SqlConnection("...."))
using (SqlCommand deleteRecord = new SqlCommand(sql, myConnection))
{
myConnection.Open();
int selectedIndex = dataGridView1.SelectedRows[0].Index;
int rowID = Convert.ToInt32(dataGridView1[0, selectedIndex].Value);
deleteRecord.Parameters.Add("@rowID", SqlDbType.Int).Value = rowID;
deleteRecord.ExecuteNonQuery();
dataGridView1.Rows.RemoveAt(selectedIndex);
}