angel7170
asked on
Data not being updated in Access Database while using the VB.net Code
Hello,
I'm trying to update records in the access database using the code as below. It's updating the data in the dataset and not in the database.
Not sure if it has to do anything with connection. please assist. Thank you
I'm trying to update records in the access database using the code as below. It's updating the data in the dataset and not in the database.
Not sure if it has to do anything with connection. please assist. Thank you
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New System.Data.OleDb.OleDbConnection
conn.ConnectionString = (My.Settings.AssetDBConnectionString)
conn.Open()
Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=@a1,CATEGORY=@a2 where (ITEM=@a1) ", conn)
cmd2.Parameters.AddWithValue("@a1", Me.txt_Item_S.Text)
cmd2.Parameters.AddWithValue("@a2", Me.cmb_Category_S.Text)
cmd2.ExecuteNonQuery()
conn.Close()
MsgBox("Record Updated Successfully")
Me.TBL_ASSETSDataGridView.Refresh()
End Sub
you can not uae "@" in access ..use "?" instead an add the parameters accordingly
You could simply not parameterize it (not the best route, but then again neither is Access for web applications):
Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=" + Me.txt_Item_S.Text + ",CATEGORY=" + Me.cmb_Category_S.Text + " where (ITEM=" + Me.txt_Item_S.Text + ") ", conn)
And remove the two cmd2.Parameter2 lines.
Dim cmd2 As New OleDb.OleDbCommand("update
And remove the two cmd2.Parameter2 lines.
Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=?,CATEGORY=? where (ITEM=?) ", conn)
cmd2.Parameters.AddWithVal ue("?", Me.txt_Item_S.Text)
cmd2.Parameters.AddWithVal ue("?", Me.cmb_Category_S.Text)
cmd2.Parameters.AddWithVal
cmd2.Parameters.AddWithVal
Or as @chapmanjw suggest
Dim OLEStr as String
OLEStr ="UPDATE TBL_ASSETS SET"
OLEStr += ITEM= '" & Me.txt_Item_S.Text & "',"
OLEStr += " CATEGORY= '" & Me.cmb_Category_S.Text & "'"
OLEStr += " where ITEM= '" & Me.txt_Item_S.Text & "'"
cmd2 = New OleDb.OleDbCommand(OLEStr, conn)
ASKER
Thanks
But I got the error message below pointing to code 'cmd2. ExecuteNonQuery() when I tried all the three suggestions given.
OleDBException was unhandled
No value given for one or more required parameters.
Please assist. Thank you
But I got the error message below pointing to code 'cmd2. ExecuteNonQuery() when I tried all the three suggestions given.
OleDBException was unhandled
No value given for one or more required parameters.
Please assist. Thank you
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New System.Data.OleDb.OleDbConnection
conn.ConnectionString = (My.Settings.AssetDBConnectionString)
conn.Open()
'Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=" + Me.txt_Item_S.Text + ",CATEGORY=" + Me.cmb_Category_S.Text + " where (ITEM=" + Me.txt_Item_S.Text + ") ", conn)
Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=?,CATEGORY=? where (ITEM=?) ", conn)
cmd2.Parameters.AddWithValue("?", Me.txt_Item_S.Text)
cmd2.Parameters.AddWithValue("?", Me.cmb_Category_S.Text)
cmd2.ExecuteNonQuery()
conn.Close()
MsgBox("Record Updated Successfully")
Me.TBL_ASSETSDataGridView.Refresh()
End Sub
you are using 3 "?" so you have to declare 3 parameters...one by one...
dim Param1 as New OledbParameter("?", Me.txt_Item_S.Text)
dim Param2 as New OledbParameter("?", Me.cmb_Category_S.Text)
dim Param3 as New OledbParameter("?", Me.txt_Item_S.Text)
cmd2.Parameters.Add(Param1 )
cmd2.Parameters.Add(Param2 )
cmd2.Parameters.Add(Param3 )
dim Param2 as New OledbParameter("?", Me.cmb_Category_S.Text)
dim Param3 as New OledbParameter("?", Me.txt_Item_S.Text)
cmd2.Parameters.Add(Param1
cmd2.Parameters.Add(Param2
cmd2.Parameters.Add(Param3
ASKER
ok, now the data is being updated in the database located in the
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management
but not in location
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management\bin\Debug
I think there's something to do with the connection string or the database in the debug folder is not getting updated whenever there is a change in the record.
But When I add a record to the table I could see the added record immediately in my datagridview but when I update it's not showing the update in the datagridview.
not sure what could cause this?
Please help.
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management
but not in location
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management\bin\Debug
I think there's something to do with the connection string or the database in the debug folder is not getting updated whenever there is a change in the record.
But When I add a record to the table I could see the added record immediately in my datagridview but when I update it's not showing the update in the datagridview.
not sure what could cause this?
Please help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm just using
this code in the update button
My.Settings.AssetDBConnect ionString
I checked my app.config file and the below location is what shown
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management
But When I use "ADD" button I don't use any hard coded to a specific location..
this code in the update button
My.Settings.AssetDBConnect
I checked my app.config file and the below location is what shown
F:\My Documents\Visual Studio 2008\Projects\Asset Management\Asset Management\Asset Management
But When I use "ADD" button I don't use any hard coded to a specific location..
ASKER
I'm running this app in "Debug" mode, also I have my dataset properties "Copy to Output Directory" as "Copy always".
But seems like it's not working..
But seems like it's not working..
you to crate a new connection string...the My.Settings.AssetDBConnect ionString must change....
what Access are u using?
what Access are u using?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Finally, it worked...Thank you very much chapmanjw and jtoutou
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New System.Data.OleDb.OleDbCon nection
conn.ConnectionString = ("Provider=Microsoft.ACE.O LEDB.12.0; Data Source=|DataDirectory|\Ass etDB.accdb ;Persist Security Info = False;")
conn.Open()
Dim cmd2 As New OleDb.OleDbCommand("update TBL_ASSETS set ITEM=?,CATEGORY=? where (ITEM=?) ", conn)
'cmd2.Parameters.AddWithVa lue("?", Me.txt_Item_S.Text)
'cmd2.Parameters.AddWithVa lue("?", Me.cmb_Category_S.Text)
Dim Param1 As New OleDb.OleDbParameter("?", Me.txt_Item_S.Text)
Dim Param2 As New OleDb.OleDbParameter("?", Me.cmb_Category_S.Text)
Dim Param3 As New OleDb.OleDbParameter("?", Me.txt_Item_S.Text)
cmd2.Parameters.Add(Param1 )
cmd2.Parameters.Add(Param2 )
cmd2.Parameters.Add(Param3 )
cmd2.ExecuteNonQuery()
conn.Close()
MsgBox("Record Updated Successfully")
TBL_ASSETSTableAdapter.Fil l(AssetDBD ataSet.TBL _ASSETS)
Me.TBL_ASSETSDataGridView. Invalidate ()
Me.TBL_ASSETSDataGridView. Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New System.Data.OleDb.OleDbCon
conn.ConnectionString = ("Provider=Microsoft.ACE.O
conn.Open()
Dim cmd2 As New OleDb.OleDbCommand("update
'cmd2.Parameters.AddWithVa
'cmd2.Parameters.AddWithVa
Dim Param1 As New OleDb.OleDbParameter("?", Me.txt_Item_S.Text)
Dim Param2 As New OleDb.OleDbParameter("?", Me.cmb_Category_S.Text)
Dim Param3 As New OleDb.OleDbParameter("?", Me.txt_Item_S.Text)
cmd2.Parameters.Add(Param1
cmd2.Parameters.Add(Param2
cmd2.Parameters.Add(Param3
cmd2.ExecuteNonQuery()
conn.Close()
MsgBox("Record Updated Successfully")
TBL_ASSETSTableAdapter.Fil
Me.TBL_ASSETSDataGridView.
Me.TBL_ASSETSDataGridView.
End Sub
ASKER
Worked as excepted