Insert statement in VB.net

[Solved/Closed]
Report
Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
-
Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
-
Hello,
I am trying to insert data from my vb.net form ( with 2 text boxes and a botton) into a data base but i get the message 'Error in sql statement'.. Please find my code below and help me out of this i am a new person in VB.

Imports System.Data.OleDb
Public Class Form1

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Try
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim mySQLString As String
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\prog3\insert_form\Database5.mdb;")
myConnection.Open()
mySQLString = "INSERT INTO myuser (username, password) VALUES('txtid.Text','txtpw.Text')"
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
End Sub
End Class


2 replies

Posts
25
Registration date
Thursday November 1, 2012
Status
Member
Last seen
March 27, 2014
2
Dear Zohaib R
I must thank you very much i am a student trying to create a database to hold records for a primarx school in my communit in Nigeria. it worked very well and i like to say a big thank you
2
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2821 users have said thank you to us this month

Posts
2368
Registration date
Sunday September 23, 2012
Status
Member
Last seen
December 13, 2018
45
Hi mikeoe2003,

There is a change needed in the Database Table. Database shouldn't have any column named as password as this is a reserved keyword. Also, when passing a value from a textbox to a database you should enclose the textbox name within '" & & "'. Change the column name to passwords in your database and replace the code as mentioned below.

Replace:

mySQLString = "INSERT INTO myuser (username, password) VALUES('txtid.Text','txtpw.Text')"

with:

mySQLString = "INSERT INTO myuser (username, passwords) VALUES('" & txtid.Text & "','" & txtpw.Text & "')"

Do reply with results.