I am trying to delete rows in database in sql from datagridview. Can you please help?
Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
SQL.DataUpdate("DELETE FROM Exp_Table2 WHERE ExpInd_ID = ??")
End Sub
I'll be that "someone less grumpy who will probably just give you all the code you need" then I guess.
As Nick said, ideally you should never concatenate strings into SQL, as any value with a single quote (') in it will break it, but not only that, hackers can do all sorts of things to your database too. I'd suggest researching SQL parameters.
But, for the sake of this question, once you've followed the link that Nick provided to get the value out of the DataGridView (There are many ways to do this, you could also use the row index along with the column index/column name, but whatever way you want to do it is fine), you'll need to assign this value to a variable of the same datatype as the value you've extracted.
The final part of that would to then use
SQL.DataUpdate("DELETE FROM Exp_Table2 WHERE ExpInd_ID = '" & yourVariable & "'")
Again, as I said - I do not advocate concatenation in SQL statements, but I used to do it too when I did my first ever project for college, so I understand we all need to start somewhere.
In future, though, do use parameters and then ask for help when you encounter a specific problem.
Parameters help to prevent against hackers using commands like DROP TABLE after inserting a single quote, which would delete the entire table (and all data in it) that the query is operating on, so always best to use parameters, which prevent this.
An example of how to use them would be as below (This is one query I have in a project I'm working on currently)
For Each dr As DataRow In dt.Rows
sql = "SELECT * FROM [Sales Headers] WHERE [Order_Number] = ? AND [Order_Cancelled] = ? AND [Stage] < ?"
cmd = New OleDbCommand(sql, con)
cmd.Parameters.Add("@num", OleDbType.Integer).Value = dr.Item("Order_Number")
cmd.Parameters.Add("@can", OleDbType.Boolean).Value = False
cmd.Parameters.Add("@stage", OleDbType.Integer).Value = 7
da = New OleDbDataAdapter(cmd)
da.Fill(commTable)
Next
DataUpdate. Is it this one:msdn.microsoft.com/en-us/library/aa302325.aspx.