Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete a data in database table

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();


        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
              
      }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading