da.Update() "syntax error" when updating database VB.NET Access 2010 - sql

I am a beginner at databases with VB.Net and am trying to update a password (field) in a database and I keep getting an error:
"Syntax error in query expression '((UID = ?) AND ((? = 1 AND Username IS NULL) OR (Username = ?)) AND ((? = 1 AND Firstname} IS NULL) OR (Firstname} = ?)) AND ((? = 1 AND Surname IS NULL) OR (Surname = ?)) AND ((? = 1 AND UPassword IS NULL) OR (UPassword = ?)) AND ((? = 1 AND DOB IS NULL)'."
Here is the code for the form it corresponds to and happens when the reset button is clicked.
Public Class ResetPW
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim sql As String
Dim dbProvider As String
Dim dbSource As String
Dim da As New OleDb.OleDbDataAdapter
Private Sub cbxShowchar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxShowchar.CheckedChanged
If cbxShowchar.Checked = True Then
txtNewpassword.PasswordChar = ""
txtVNewpassword.PasswordChar = ""
Else
txtNewpassword.PasswordChar = "*"
txtVNewpassword.PasswordChar = "*"
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Me.Close()
Form2.Show()
End Sub
Private Sub ResetPW_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbSource = "DATA Source = C:\Users\Luke\Dropbox\FdSc Computing\Year 2\Project\Product\Book Database.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM T_Users"
da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Users")
MsgBox("The database is now open")
con.Close()
MsgBox("The database is now closed")
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
If txtNewpassword.Text = txtVNewpassword.Text Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Users").Rows(0).Item(4) = txtNewpassword.Text
da.Update(ds, "Users") 'ERROR HAPPENS HERE'
MsgBox("Password Reset", vbInformation)
Else
MsgBox("The passwords do not match", vbExclamation)
End If
End Sub
End Class
I would be very grateful for any help. Thanks guys!

Related

how to search and update data into ms access

my problem is how to search data and update it in ms access and vb.net
my code:
Imports System.Data.OleDb
Public Class Form5
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form5_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Admin\Desktop\project database\Database5.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
myConnection.Open()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox10.Clear()
TextBox11.Clear()
Dim str As String
str = "SELECT * FROM registration WHERE (Name = '" & TextBox1.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
TextBox2.Text = dr("No").ToString
TextBox3.Text = dr("Name").ToString
TextBox4.Text = dr("Age").ToString
TextBox5.Text = dr("Gender").ToString
TextBox6.Text = dr("Phone").ToString
TextBox7.Text = dr("Address").ToString
TextBox8.Text = dr("Desease").ToString
TextBox9.Text = dr("RoomNo").ToString
TextBox10.Text = dr("Building").ToString
TextBox11.Text = dr("RoomType").ToString
End While
myConnection.Close()
End Sub
End Class
the error I'm getting:
"No value given for one or more required parameters."}
Isn't the issue here that when you click button3 you clear the textbox1 where I assume you type the name you wish to search. So its throwing you an error that there is nothing in that field. And even if you type something after wards, and click the button again, it will clear it again before it starts searching. so basically, you're searching for nothing.

Copy System.Data.Datatable to Access DB Programmatically Vb.net [duplicate]

Can someone please explain why, when I click the "Commit" button, I get the error
Syntax error in INSERT INTO statement.
Here's the code.
Public Class Form3
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
con.Open()
sql = "SELECT * from TA_OFFICER"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TA_Officers")
con.Close()
MaxRows = ds.Tables("TA_Officers").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDel.Enabled = False
txtPosition.Clear()
txtLName.Clear()
txtFName.Clear()
txtMInitial.Clear()
txtAddress.Clear()
txtCourse.Clear()
txtEmail.Clear()
txtContact.Clear()
End Sub
Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
Label2.Visible = False
txtPosition.Visible = False
End Sub
Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
Label2.Visible = True
txtPosition.Visible = True
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text
da.Update(ds, "TA_Officers")
MsgBox("Data Updated!")
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "TA_Officers")
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("TA_Officers").NewRow()
dsNewRow.Item("Firstname") = txtFName.Text
dsNewRow.Item("Middleinitial") = txtMInitial.Text
dsNewRow.Item("Lastname") = txtLName.Text
dsNewRow.Item("Mobilenumber") = txtContact.Text
dsNewRow.Item("Emailaddress") = txtEmail.Text
dsNewRow.Item("Position") = TxtPosition.Text
dsNewRow.Item("Course") = txtCourse.Text
dsNewRow.Item("Address") = txtAddress.Text
ds.Tables("TA_Officers").Rows.Add(dsNewRow)
**da.Update(ds, "TA_Officers")**
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
End If
End Sub
Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
Form2.Show()
Me.Close()
End Sub
Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TA_MEMBERSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)
End Sub
End Class
Position is a reserved word for JET 4.0 - try to rename the column something else such as prog_position. Access will often let you create the column with a reserved name, but when you go to access it in code JET blocks it and gives you a generic Syntax error in INSERT INTO Statement error without being specific. Check out http://support.microsoft.com/kb/248738 for more information on reserved words. It is goods practice to always prefix your column names with something short so that you never run into conflicts or reserved word issues.
As Makita mentions, your problem stems from the fact that Position is a reserved word in Jet/ACE SQL. The solution is to add the following two lines of code after you create the OleDbCommandBuilder object:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the OleDbCommandBuilder to generate SQL statements like this
INSERT INTO [TA_OFFICER] ([FirstName], ...
...instead of
INSERT INTO TA_OFFICER (FirstName, ...
Wrapping the Position column name in those square brackets will tell the Jet database engine that it is a column name, not a keyword.
This kind of error can happen when you have different data type in the code and in the DB.
Checking the log can give some light on this.

Identifier expected? visual basic

im almost done with this and then that... hopefully someone will be able to help me (Really hope so atleast, cus i need to be done with this:P) (need more text cus there is to much code):
|error is there|>
Private Sub FlatButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlatButton1_Click. <|error is there|
Dim Conn As New MySqlConnection("Not going to publish this")
If FlatTextBox1.Text = "" Then
MsgBox("No username specified")
FlatTextBox2.Text = ""
Else
If FlatTextBox2.Text = "" Then
MsgBox("No password specified")
FlatTextBox1.Text = ""
Else
Try
Me.Text = "Logging in..."
Conn.Open()
Dim sqlquery As String = "SELECT * FROM Testing WHERE Username = '" & FlatTextBox1.Text & "';"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = Conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() = True Then
If data(2).ToString = FlatTextBox2.Text Then
Me.Text = "Logged in!"
My.Settings.Username = FlatTextBox1.Text
MsgBox("Welcome " + data(1).ToString)
Home.Show()
Me.Close()
If data(3).ToString = "1" Then
My.Settings.Admin = "Yes"
Else
My.Settings.Admin = "No"
End If
End If
Else
MsgBox("Failed Login")
End If
End While
Catch ex As Exception
End Try
End If
End If
End Sub
End Class
Replace these Lines
Private Sub FlatButton1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles FlatButton1_Click
With
Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1_Click

Validating a command according to user level

I am trying to use this code to validate the delete operation. It should only execute if the code entered in the input box is for a super admin but it is returning an error "There is no row at position 1". Anyone who can give a better code structure will be greatly appreciated.
Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb;Persist Security Info=False"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "select UserID from Users where UserID = 'dlass8504'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds2, "IBCARIP")
con.Close()
If InputBox("Please input your UserID to complete operation").ToString <> ds2.Tables("IBCARIP").Rows(inc).Item("UserID").ToString Then
MsgBox("You do not posess sufficient previlegies to perfom this operation..!", MsgBoxStyle.Critical)
ElseIf MsgBox("Do you really want to Delete this Record?", MsgBoxStyle.YesNo) = MsgBoxResult.Ok Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("IBCARIP").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "IBCARIP")
navigaterecords()
End If
End Sub
For idea only ..
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'here's code for retrieving level
btnD.Enabled = iif(Level < 3, True, False) '-- this will disabled your delete button
End Sub
Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
Dim r As MsgBoxResult
r = Msgbox("Do you really want to Delete this Record?", MsgBoxStyle.YesNo)
If r = MsgBoxResult.Yes Then
'erasing here
End If
End Sub

The ConnectionString property has not been initialized help databases

I have been trying to work this out for the last 4 weeks and cannot find the answer I've just been going around in circles. I have had multiple errors with con As New OleDb.OleDbConnection and connection string and ISAM's
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Public sql As String
Dim ds As New DataSet
Dim dbPassword As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "C:\Users\Edward\Desktop\Edward\DataBase Login Window\Users.mdb;Jet OLEDB:Database"
con.ConnectionString = dbProvider & dbSource
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = "SELECT * FROM users WHERE accUsername='" & TextBox1.Text & "'AND accPass='" & TextBox2.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "users")
If ds.Tables("users").Rows.Count = 1 Then
Dim isAdmin As Boolean = ds.Tables("users").Rows(0).Item(4)
If isAdmin = True Then
MsgBox("Logged in.")
Form4.Show()
Form4.Username = TextBox1.Text
Form4.Password = TextBox2.Text
ElseIf isAdmin = False Then
MsgBox("Logged in user.")
Form6.Show()
Form6.Username = TextBox1.Text
Form6.Password = TextBox2.Text
End If
Else
MsgBox("Incorrect Username or Password")
End If
Me.Hide()
ds.Clear()
Me.Refresh()
End Sub
Private Sub createanaccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createanaccount.Click
Form2.ShowDialog()
End Sub
End Class
in the button you should set the connectionString here is an example.
dim con as new SQLConnection/OLEDBConnection
con.ConnectionString="Provide your connection string"
con.open
I hope this will definitely solve your problem