Rickson
Rickson

Reputation: 26

Why Cant i delete rows in a table

I cant delete rows in a table it doesn't come up with any error messages con has been used on other statements and works just fine textbox1 is read-only and gets input from a listbox filled with the values of the "Driver" column and check() is just to refresh the listbox containing the values

        con.Open()

    Try
        success = True
        Dim cmd As New SqlCommand("DELETE FROM Driver WHERE Driver='" & TextBox1.Text & "';", con)
    Catch ex As Exception
        MsgBox(ex.Message)
        success = False
    End Try
    If success Then
        MsgBox("Success")
    End If
    con.Close()
    check()

Upvotes: 0

Views: 129

Answers (1)

Igor
Igor

Reputation: 62318

You never execute the command. Add a call to SqlCommand::ExecuteNonQuery

Dim cmd As New SqlCommand("DELETE FROM Driver WHERE Driver='" & TextBox1.Text & "';", con)
cmd.ExecuteNonQuery() ' added

That said you should not use string concatenation when adding values to your sql statement. Instead use Parameters which prevents sql injection attacks.

Dim cmd As New SqlCommand("DELETE FROM Driver WHERE Driver= @driver", con)
cmd.Parameters.Add("@driver", SqlDbType.VarChar).Value = TextBox1.Text ' add parameter
cmd.ExecuteNonQuery() ' added

Assumption

Driver is a column inside a table with the same name Driver. If this is not the case then you do not understand tables and columns or the DELETE statement which in its basic form is DELETE FROM [TABLE] WHERE [condition on one or more columns]

Upvotes: 2

Related Questions