0

Delete datagridview cell click event it says the information was deleted but after clicking the pop-out message the information was still there even if I deleted that certain information. Please help

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string maincon = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
            SqlConnection sqlcon = new SqlConnection(maincon);
            int rfidno = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["rfidno"].FormattedValue.ToString());

            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                sqlcon.Open();
                da.DeleteCommand = new SqlCommand("delete tbl_registerStudent where rfidno = '" + rfidno + "'", sqlcon);
                da.DeleteCommand.ExecuteNonQuery();

                MessageBox.Show("" + rfidno);
                MessageBox.Show("Delete Successfull");
                sqlcon.Close();
                bindGrid();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

This is the code of the bindGrid

public void bindGrid()
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;

        }
    ```

2 Answers2

0

Try This:

        try
        {
            SqlDataAdapter da = new SqlDataAdapter();
            sqlcon.Open();
            da.DeleteCommand = new SqlCommand("delete from tbl_registerStudent where rfidno = '" + rfidno + "'", sqlcon);
            da.DeleteCommand.ExecuteNonQuery();

            MessageBox.Show("" + rfidno);
            MessageBox.Show("Delete Successfull");
            sqlcon.Close();
            bindGrid();
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
  • Hello. I am using the code that you've shared but the result that I'm getting is the same as before – JohnSanity COPH Oct 25 '20 at 10:11
  • Hi Johh, In that case, please check the value of ExecuteNonQuery() if it is -1 means it is not successful. Try deleting the same from SQL query to check if you get any constrains error. – Prashanth Madakatte Oct 25 '20 at 11:31
0

Initially, you have to open the SqlConnection, then execute your command, bind the result and last to not forget to close the connection:

public void bindGrid()
{
   SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True");
   con.Open()
   SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
   SqlDataAdapter sda = new SqlDataAdapter(cmd);
   DataTable dt = new DataTable();
   sda.Fill(dt);
   dataGridView1.DataSource = dt;
   con.Close();
}

A better approach, in order you don't remember to close the SqlConnection is to use a using statement and let the compiler to generate this call for you.

public void bindGrid()
{
   // This should be normally placed in a configuration file and read it from there. 
   string connectionString = @"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True";

   using(var sqlConnection = new SqlConnection(connectionString))
   {
       con.Open()
       SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
       SqlDataAdapter sda = new SqlDataAdapter(cmd);
       DataTable dt = new DataTable();
       sda.Fill(dt);
       dataGridView1.DataSource = dt;  
   }
}

Furthermore, since SqlCommand class inherits from DbCommand and the latter implements the IDisposable interface, you should keep in mind that you should use once more the using statement and used resources to be released. For furthermore info on this, please check this.

That being said the suggested approach is the following:

public void bindGrid()
{
   // This should be normally placed in a configuration file and read it from there. 
   string connectionString = @"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True";

   using(var sqlConnection = new SqlConnection(connectionString))
   {
       con.Open()
       using(var cmd = new SqlCommand("Select * from tbl_registerStudent", con))
       {
           SqlDataAdapter sda = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           dataGridView1.DataSource = dt;  
       }
    }
}
Christos
  • 53,228
  • 8
  • 76
  • 108
  • Hi Mr. Christos. I've used the code that you shared which is the code #1and #2. It is working for me but I still got the same results as before sad to say – JohnSanity COPH Oct 26 '20 at 05:54
  • @JohnSanityCOPH Hi ! One question, could you please place a breakpoint at `dataGridView1.DataSource = dt;` and verify that the data are loaded from the database ? It they are loaded, then we should look somewhere else for finding the solution of the problem. Otherwise, we don't have the data, and we can't tell. Thanks – Christos Oct 26 '20 at 06:55
  • Hi @Christos! I've located the problem now and its solved. The problem is in my database the data type of the "rfidno" is varchar and i put was "int" upon changing the int to string it solved. Thank you for the comment it solved my problem – JohnSanity COPH Nov 01 '20 at 02:17
  • Hi @JohnSanityCOPH ! Sounds good. You found it :) Kudos. – Christos Nov 01 '20 at 06:16