Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim cmd As New OleDb.OleDbCommand
       If MainAdmin.DataGridView4.Rows.Count > 0 Then
           If MainAdmin.DataGridView4.SelectedRows.Count > 0 Then
               Dim strStdID As String = MainAdmin.DataGridView4.SelectedRows(0).Cells("Username").Value
               'open connection
               If Not cnn.State = ConnectionState.Open Then
                   cnn.Open()
               End If
               'delete data
               cmd.Connection = cnn
               cmd.CommandText = "DELETE FROM DAdmin WHERE Username =" & strStdID
               cmd.ExecuteNonQuery()
           End If
       End If
       cnn.Close()


What I have tried:

Please help me debug this line of codes
Posted
Updated 1-Mar-17 6:32am
Comments
[no name] 1-Mar-17 11:51am    
Debug it yourself. We don't have your code, your forms, your project, your database, so we can't debug it for you.
Bryian Tan 1-Mar-17 11:57am    
Make sure Username column is in the database

If strStdID is "Joe Bloggs" then what SQL are you executing?

DELETE FROM DAdmin WHERE Username = Joe Bloggs


Is that valid SQL? What if your username was "me or 1=1" then your SQL would be

DELETE FROM DAdmin WHERE Username = me or 1 = 1

what would happen then? (Google "little bobby tables" for a clue).

You need to put text parameters in quotes, however the best way of doing this is to use parameters and let ado.net sort it out for you

cmd.CommandText = "DELETE FROM DAdmin WHERE Username = @userID";
cmd.Parameters.AddWithValue ("@userID", strStdID);
cmd.ExecuteNonQuery()


If you still get the error it could be because Username is a reserved word so you might need to use square brackets to let the database know you mean the column name and not the reserved word

cmd.CommandText = "DELETE FROM DAdmin WHERE [Username] = @userID";
 
Share this answer
 
Comments
CaptainChizni 1-Mar-17 13:17pm    
Thank you it works btw from now on i will use parameterized queries
You should be using Parameterized Queries[^] to prevent SQL injection[^], and it will also eliminate the error which is the missing quotes around 'strStdID', i.e.
cmd.CommandText = "DELETE FROM DAdmin WHERE Username ='" & strStdID & "'"
 
Share this answer
 
Looks like you forgot to tell that strStdID is a string
VB
cmd.CommandText = "DELETE FROM DAdmin WHERE Username = '" & strStdID & "'"
 
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