Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, Guys, I hope you will be fine.I am facing another issue in my project and now that is when I delete values so I get an error I am using a checkbox for selected multiple rows by data grid view and delete all selected rows but when I click the button so sometimes it delete but mostly I will get an error.

What I have tried:

C#
foreach (DataGridViewRow item in this.dataGridView1.Rows)
            {
                if (bool.Parse(item.Cells[1].Value.ToString()))
                {
                    con_string.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = con_string;
                    command.CommandText = "Delete from [Runtime] Where [Values] = " + item.Cells[0].Value.ToString() + "";
                    command.ExecuteNonQuery();
                    con_string.Close();
                }
            }
Posted
Updated 15-Jun-17 18:10pm
v2

1 solution

One problem is that you concatenate the values directly to the SQL statement. This introduces conversion errors, leaves you open to SQL injections and so on.

The fix would be to use OleDbParameter objects. While the article is using SqlParamater, the idea is the same so have a look at Properly executing database operations[^]
 
Share this answer
 
v2
Comments
Member 9983063 16-Jun-17 0:14am    
i have also tried oledbparams
Wendelius 16-Jun-17 0:25am    
What was the error you got with them?
Member 9983063 16-Jun-17 6:18am    
same error
Wendelius 16-Jun-17 8:04am    
On which line?
Member 9983063 16-Jun-17 8:13am    
in this line

if (bool.Parse(item.Cells[1].Value.ToString()))

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