I have this form that takes these information from user @ID,@From,@To ,@Title ,@Message. Then it insert them on a database table .
How can I delete a certain data using the @ID,
user will have a dataGridView1 with all the datatable, then by getting the CurrentCell it will delete this certain data.
private void delet_button_Click(object sender, EventArgs e)
{
int i = dataGridView1.CurrentCell.RowIndex;
string strid = dataGridView1[0, i].Value.ToString();
//MessageBox.Show(strid);
int id = Convert.ToInt32(strid);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete * from MessagesTable where [ID]=@ID ";
cmd.Parameters.AddWithValue("@ID", id);
MessageBox.Show("Message was deleted");
BindGrid();
}
>Solution :
Add cmd.ExecuteNonQuery();
private void delet_button_Click(object sender, EventArgs e)
{
int i = dataGridView1.CurrentCell.RowIndex;
string strid = dataGridView1[0, i].Value.ToString();
//MessageBox.Show(strid);
int id = Convert.ToInt32(strid);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete from MessagesTable where [ID]=@ID ";
cmd.Parameters.AddWithValue("@ID", id);
cmd.ExecuteNonQuery();
MessageBox.Show("Message was deleted");
BindGrid();
}