0

I am trying to delete a row of data in DataGridView and SQL Server database. On the first click on the delete button it shows

MessageBox.Show("Record Deleted Successfully!");

but the selected row was not deleted in DataGridView and in the database.

This is my code:

private void PayratesDisplay()
{
    con.Open();

    DataTable dt = new DataTable();
    adap = new SqlDataAdapter("select Membership AS [Membership Type], PerSession AS [Per Session], Discounted from tbl_payrates", con);

    adap.Fill(dt);

    dataGridView4.DataSource = dt;

    con.Close();
}

// Clear Data  
private void ClearData()
{
    tbMemship.Text = "";
    tbPerses.Text = "";
    tbDisc.Text = "";
    ID = 0;
}

private void btn_PRdel_Click(object sender, EventArgs e)
{
    if (ID != 0)
    {
        con.Open();

        cmd = new SqlCommand("DELETE FROM tbl_payrates WHERE Membership = @Membership", con);
        cmd.Parameters.AddWithValue("@Membership", ID);

        cmd.ExecuteNonQuery();

        con.Close();

        MessageBox.Show("Record Deleted Successfully!");

        PayratesDisplay();
        ClearData();
    }
}  
10
  • How are you assigning the value for ID? Commented Oct 1, 2015 at 3:35
  • i declared the in the partial class Load : Form int ID = 1; Commented Oct 1, 2015 at 3:38
  • So you're always deleting values where Membership = 1? When you click the delete button, does the ID have the correct value? Commented Oct 1, 2015 at 3:39
  • 2
    What integer value do you get in return from cmd.ExecuteNonQuery()? If you are getting 0 that means there are no rows affected by your delete query. Commented Oct 1, 2015 at 3:40
  • @FelixPamittan i want to delete the selected row with Membership=any value do i need to change int ID =1; to ID=0? Commented Oct 1, 2015 at 3:43

2 Answers 2

1

If I understand correctly, you are trying to delete the current row of the grid. Then the ID member is not needed and you can use something like this

private void btn_PRdel_Click(object sender, EventArgs e)
{
    var row = dataGridView4.CurrentRow;
    if (row == null) return;
    var ID = row.Cells["Membership Type"].Value;
    // The rest of the code (w/o the "if ID != 0")
}
Sign up to request clarification or add additional context in comments.

5 Comments

int ID = (int)row.Cells["Membership Type"].Value; this line has an error which is InvalidCastException
@Josh Hmm, what it is then - string? Let me update the answer. But that's what you are trying to do, right?
I am deleting a row with integers in it
@Josh Hmm, that couldn't be true - both select and delete sql statements operate on tbl_payrates and the delete is filtering by Membership column, which also is included in the grid (just with alias), so its either an int and the original cast should succeed or not an int.
the column Discounted is in varchar(50)
0

Change your code like this

int r=cmd.ExecuteNonQuery();
con.Close();
if(r>0){MessageBox.Show("Record Deleted Successfully!");}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.