error in my delete string?

jnash

Well-known member
Joined
Oct 20, 2006
Messages
111
Programming Experience
Beginner
hi there im trying to get my delete statement to work but it keeps on erroring on executequery, is this is the wrong way to do this


VB.NET:
    Dim TheConnection As New MySqlConnection("server=localhost;" _
        & "user id=root;" _
        & "password=password;" _
        & "database=supervid")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cmd As New MySqlCommand
        cmd.CommandType = System.Data.CommandType.Text
        cmd.CommandText = "DELETE FROM tblMember WHERE MemberNo ='" & txtMember.Text & "'"""
        TheConnection.Open()
        cmd.ExecuteNonQuery()
        TheConnection.Close()

        MessageBox.Show("Removed From System!")
    End Sub
 
If the field MemberNo is of integer type you don't need the single quotes.

Either way you have a single quote and a double quote at the end of the commandText:
VB.NET:
'if MemberNo is of type string
'    "'"""  [B]should be[/B] "'"
cmd.CommandText = "DELETE FROM tblMember WHERE MemberNo='" & txtMember.Text & "'"

'if MemberNo is type integer
"DELETE FROM tblMember WHERE MemberNo=" & cInt(txtMember.Text)
 
In situations like this, use the Debugger to have a look at the contents of the string

You might well find it looks something like this:

DELETE FROM tblMember WHERE MemberName='john o'brien'"


There's two problems here - one a trailing "
and the other, caused by using string concatenation to build sql strings (Never, ever, ever use concatenation to build sql strings. Learn about parameterized sqls)
 
Back
Top