Using delete statement in VB.net

Solved/Closed
mikeoe2003 Posts 24 Registration date Thursday November 1, 2012 Status Member Last seen March 27, 2014 - Nov 1, 2012 at 10:24 AM
 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

2 responses

Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Nov 2, 2012 at 02:40 AM
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 again Zohaib R for zour kind and prompt assistance
0