Using delete statement in VB.net [Solved/Closed]

Report
Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
-
 Zohaib R -
Hello,
I am trying to delet records from a table, when I click the btnDelete button nothing happens no error message and no record is deleted please some help. My code is below

Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Form1

Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
Try
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim mySQLString As String

con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\prog3\insert_form \Database5.mdb;")

con.Open()
mySQLString = "DELETE FROM myuser WHERE " & _
"username='txtid.Text' AND passwords='txtpw.Text'"


cmd = New OleDbCommand(mySQLString, con)
cmd.ExecuteNonQuery()
cmd = New OleDbCommand(mySQLString, con)

Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
txtid.Text = ""
txtpw.Text = ""

End Sub

End Class

2 replies

Posts
2398
Registration date
Sunday September 23, 2012
Status
Moderator
Last seen
December 13, 2018
523
Hi mikeoe2003,

As I mentioned earlier in your post, when referring to a text box you must enclose the textbox name in ` " & & " `.

Replace:

mySQLString = "DELETE FROM myuser WHERE " & _
"username='txtid.Text' AND passwords='txtpw.Text'"

With:

mySQLString = "DELETE FROM myuser WHERE " & _
"username='" & txtid.Text & "' AND passwords='" & txtpw.Text & "'"

Please reply if you have any further questions.

thank again Zohaib R for zour kind and prompt assistance

Newsletter

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!