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

mikeoe2003 25 Posts Thursday November 1, 2012Registration date March 27, 2014 Last seen - Nov 1, 2012 at 10:24 AM - Latest reply:  Zohaib R
- Nov 2, 2012 at 04:34 AM
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

See more 

2 replies

Zohaib R 2421 Posts Sunday September 23, 2012Registration date July 16, 2018 Last seen - Nov 2, 2012 at 02:40 AM
0
Thank you
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.

0
Thank you
thank again Zohaib R for zour kind and prompt assistance