Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
there is no error message in my code, but not delete the record. if there is any error in logic,please guide me:the code is below:

What I have tried:

private void button2_Click(object sender, EventArgs e)
        {
            con.Open();
            
            OleDbCommand delcmd = new OleDbCommand();
            //if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count-1)
            if(dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Index!=dataGridView1.Rows.Count-1)
            
            {
                delcmd.CommandText = "delete from table123 where Idno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
               
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
             
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
                bind();
                clear();
            }
else
{
Messagebox.show("can’t delete");
}
con.close();
                    }
Posted
Updated 23-Feb-17 2:00am
Comments
Graeme_Grant 23-Feb-17 5:19am    
Have you set a breakpoint on the following line and checked the value being passed before execution? Is the correct value being passed?
delcmd.CommandText = "delete from table123 where Idno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
Member 13006682 23-Feb-17 5:26am    
i have 2 label& textbox as Name & Idno,how i'm give input for the above condition
Graeme_Grant 23-Feb-17 5:30am    
Not what I asked. Do you know how to set a breakpoint for debugging purposes?
Member 13006682 23-Feb-17 5:32am    
i'don't know.please told about how to set break point for debugging

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Second, if nothing gets deleted, it's because the WHERE clause of your SQL fails to match. That may be that you have the wrong column, the wrong datagridview, or that the data is not what you expect - we can't tell, because we don't have your app running and can't access your DB anyway.
So use the debugger to find out exactly what you are passing to SQL, and then manually query your DB to find any matching rows. If none exist - and they don't - you need to look at why. But we can't do any of that for you!
 
Share this answer
 
This video will help you get started with debugging: Basic Debugging with Visual Studio 2010 - YouTube[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900