0

Apologies in advance if this is a mundane question or has a lack of information, I am new to both software development and the stack overflow community.

For clarification purposes:

  • I am using the AdventureWorks Sample Database in SQL Server 2012 and I have created a table named Users.
  • I am using C#

Overview of what im trying to achieve and the problem:

The user should be able to select a row in the datagridview and select the delete button to delete the chosen user from the database. When running my code the row is deleted from the datagridview but not the database and as upon refreshing the datagridview the record returns and I'm not quite sure where I have gone wrong ..

    private void btnDelete_Click(object sender, EventArgs e)
    {
        delete_record();

        SqlConnection conn = new SqlConnection();
        SqlCommand delcmd = new SqlCommand();
        conn.ConnectionString = "server=localhost;Trusted_connection=yes;database=AdventureWorks2012";
        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Users", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dbgrid1.DataSource = dt;
        conn.Close();
    }


    private void delete_record()
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand delcmd = new SqlCommand();
        conn.ConnectionString = "server=localhost;Trusted_connection=yes;database=AdventureWorks2012";
        if (dbgrid1.Rows.Count > 1 && dbgrid1.SelectedRows[0].Index != dbgrid1.Rows.Count - 1)
        {
            delcmd.CommandText = "DELETE FROM Users WHERE ID=" + dbgrid1.SelectedRows[0].Cells[0].Value.ToString() + "";
            conn.Open();
            delcmd.Connection = conn;
            delcmd.ExecuteNonQuery();
            conn.Close();
            dbgrid1.Rows.RemoveAt(dbgrid1.SelectedRows[0].Index);
            MessageBox.Show("deleted");
        }
    }
Chad Bonthuys
  • 2,029
  • 4
  • 27
  • 35

1 Answers1

1

In your code

dbgrid1.Rows.RemoveAt(dbgrid1.SelectedRows[0].Index);

The above code removes data from grid, and is working fine

But the below code not working properly (i e SQL query to delete row)

delcmd.CommandText = "DELETE FROM Users WHERE ID=" + dbgrid1.SelectedRows[0].Cells[0].Value.ToString() + "";

means ,The ID you are taking dbgrid1.SelectedRows[0].Cells[0].Value.ToString() is wrong.

Take It in a Watch window and find out what value is coming there.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pravprab
  • 2,301
  • 3
  • 26
  • 43
  • Thank you for the quick reply, I have identified the problem as the row ID passed in is not the same as the ID field in the database. The ID field is a auto increment integer. Is it possible to delete records via row ID ? I tried the below: \code\ SqlParameter paramid = new SqlParameter("@ID", SqlDbType.Int); paramid.Value = dbgrid1.SelectedRows[0].Index.ToString(); delcmd.Parameters.Add(paramid); delcmd.CommandText = "DELETE FROM Users WHERE row_number() over (order by id)=@ID"; \code\ – Chad Bonthuys Jan 27 '14 at 12:39