Update isn't updating
tattoo wrote:
There is something very basic that I am missing here. When I change the data in a field it isn't getting updated back to the Access Db. I'm using Access 2000 and VB.Net 2003.
To try and troubleshoot where I'm going wrong I have put together a very minor application and selected just one field from the Db table to update. Below is all the code I'm using.
I've tried reading the forums and the MSDN help and I seem to get multiple, different descriptions on how to update but nothing is working.
any help is appreciated. I have tried binding the field to the Db and tried without binding as well.
PublicClass Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
PublicSubNew()
MyBase.New()
'This call is required by the Windows Form Designer.InitializeComponent()
'Add any initialization after the InitializeComponent() callEndSub'Form overrides dispose to clean up the component list.ProtectedOverloadsOverridesSub Dispose(ByVal disposingAsBoolean)
If disposingThenIfNot (componentsIsNothing)Thencomponents.Dispose()
EndIfEndIfMyBase.Dispose(disposing)
EndSub'Required by the Windows Form DesignerPrivate componentsAs System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form DesignerFriendWithEvents btnNextAs System.Windows.Forms.Button
FriendWithEvents btnPrevAs System.Windows.Forms.Button
FriendWithEvents lblNavLocationAs System.Windows.Forms.Label
FriendWithEvents TextBox1As System.Windows.Forms.TextBox
FriendWithEvents Label1As System.Windows.Forms.Label
FriendWithEvents btnQuitAs System.Windows.Forms.Button
FriendWithEvents btnUpdateAs System.Windows.Forms.Button
FriendWithEvents Adapt1As System.Data.OleDb.OleDbDataAdapter
FriendWithEvents OleDbSelectCommand1As System.Data.OleDb.OleDbCommand
FriendWithEvents OleDbInsertCommand1As System.Data.OleDb.OleDbCommand
FriendWithEvents OleDbUpdateCommand1As System.Data.OleDb.OleDbCommand
FriendWithEvents OleDbDeleteCommand1As System.Data.OleDb.OleDbCommand
FriendWithEvents Conn1As System.Data.OleDb.OleDbConnection
FriendWithEvents DataSet1As Udate2.DataSet1
FriendWithEvents StatusBar1As System.Windows.Forms.StatusBar
FriendWithEvents TextBox2As System.Windows.Forms.TextBox
FriendWithEvents Label2As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()>PrivateSub InitializeComponent()
Me.btnNext =New System.Windows.Forms.Button
Me.btnPrev =New System.Windows.Forms.Button
Me.lblNavLocation =New System.Windows.Forms.Label
Me.TextBox1 =New System.Windows.Forms.TextBox
Me.DataSet1 =New Udate2.DataSet1
Me.Label1 =New System.Windows.Forms.Label
Me.btnQuit =New System.Windows.Forms.Button
Me.btnUpdate =New System.Windows.Forms.Button
Me.Adapt1 =New System.Data.OleDb.OleDbDataAdapter
Me.OleDbDeleteCommand1 =New System.Data.OleDb.OleDbCommand
Me.Conn1 =New System.Data.OleDb.OleDbConnection
Me.OleDbInsertCommand1 =New System.Data.OleDb.OleDbCommand
Me.OleDbSelectCommand1 =New System.Data.OleDb.OleDbCommand
Me.OleDbUpdateCommand1 =New System.Data.OleDb.OleDbCommand
Me.StatusBar1 =New System.Windows.Forms.StatusBar
Me.TextBox2 =New System.Windows.Forms.TextBox
Me.Label2 =New System.Windows.Forms.Label
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
''btnNext'Me.btnNext.Location =New System.Drawing.Point(360, 208)
Me.btnNext.Name = "btnNext"
Me.btnNext.Size =New System.Drawing.Size(72, 40)
Me.btnNext.TabIndex = 0
Me.btnNext.Text = ">"
''btnPrev'Me.btnPrev.Location =New System.Drawing.Point(184, 208)
Me.btnPrev.Name = "btnPrev"
Me.btnPrev.Size =New System.Drawing.Size(72, 40)
Me.btnPrev.TabIndex = 1
Me.btnPrev.Text = "<"
''lblNavLocation'Me.lblNavLocation.Font =New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,CType(0,Byte))
Me.lblNavLocation.Location =New System.Drawing.Point(256, 208)
Me.lblNavLocation.Name = "lblNavLocation"
Me.lblNavLocation.Size =New System.Drawing.Size(104, 40)
Me.lblNavLocation.TabIndex = 26
Me.lblNavLocation.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
''TextBox1'Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text",Me.DataSet1, "Vehicle Info.Department"))
Me.TextBox1.Font =New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,CType(0,Byte))
Me.TextBox1.Location =New System.Drawing.Point(216, 96)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size =New System.Drawing.Size(144, 27)
Me.TextBox1.TabIndex = 27
Me.TextBox1.Text = ""
''DataSet1'Me.DataSet1.DataSetName = "DataSet1"
Me.DataSet1.Locale =New System.Globalization.CultureInfo("en-US")
''Label1'Me.Label1.Font =New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,CType(0,Byte))
Me.Label1.Location =New System.Drawing.Point(96, 96)
Me.Label1.Name = "Label1"
Me.Label1.Size =New System.Drawing.Size(104, 32)
Me.Label1.TabIndex = 28
Me.Label1.Text = "Department"
''btnQuit'Me.btnQuit.Location =New System.Drawing.Point(320, 272)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.Size =New System.Drawing.Size(112, 24)
Me.btnQuit.TabIndex = 29
Me.btnQuit.Text = "Quit"
''btnUpdate'Me.btnUpdate.Location =New System.Drawing.Point(184, 272)
Me.btnUpdate.Name = "btnUpdate"
Me.btnUpdate.Size =New System.Drawing.Size(112, 24)
Me.btnUpdate.TabIndex = 30
Me.btnUpdate.Text = "Update"
''Adapt1'Me.Adapt1.DeleteCommand =Me.OleDbDeleteCommand1
Me.Adapt1.InsertCommand =Me.OleDbInsertCommand1
Me.Adapt1.SelectCommand =Me.OleDbSelectCommand1
Me.Adapt1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Vehicle Info",New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("Department", "Department"),New System.Data.Common.DataColumnMapping("Vehicle ID #", "Vehicle ID #")})})
Me.Adapt1.UpdateCommand =Me.OleDbUpdateCommand1
''OleDbDeleteCommand1'Me.OleDbDeleteCommand1.CommandText = "DELETE FROM [Vehicle Info] WHERE ([Vehicle ID #] = ?)AND (Department = ?OR ?IS" & _
" NULLAND DepartmentIS NULL)"
Me.OleDbDeleteCommand1.Connection =Me.Conn1
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Vehicle_ID__", System.Data.OleDb.OleDbType.SmallInt, 0, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Vehicle ID #", System.Data.DataRowVersion.Original,Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Department", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Department", System.Data.DataRowVersion.Original,Nothing))
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Department1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Department", System.Data.DataRowVersion.Original,Nothing))
''Conn1'Me.Conn1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=1;Jet OLEDB:Database Password=;Data Source=""C:\Documentsand Setting" & _
"s\Kevin\Desktop\SB\SB47.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global " & _
"Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database" & _
"=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Da" & _
"tabase Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Loc" & _
"ale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admi" & _
"n;Jet OLEDB:Encrypt Database=False"
''OleDbInsertCommand1'Me.OleDbInsertCommand1.CommandText = "INSERT INTO [Vehicle Info] (Department, [Vehicle ID #]) VALUES (?, ?)"
Me.OleDbInsertCommand1.Connection =Me.Conn1
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Department", System.Data.OleDb.OleDbType.VarWChar, 50, "Department"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Vehicle_ID__", System.Data.OleDb.OleDbType.SmallInt, 0, "Vehicle ID #"))
''OleDbSelectCommand1'Me.OleDbSelectCommand1.CommandText = "SELECT Department, [Vehicle ID #] FROM [Vehicle Info]"
Me.OleDbSelectCommand1.Connection =Me.Conn1
''OleDbUpdateCommand1'Me.OleDbUpdateCommand1.CommandText = "UPDATE [Vehicle Info]SET Department = ?, [Vehicle ID #] = ? WHERE ([Vehicle ID #" & _
"] = ?)AND (Department = ?OR ?IS NULLAND DepartmentIS NULL)"
Me.OleDbUpdateCommand1.Connection =Me.Conn1
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Department", System.Data.OleDb.OleDbType.VarWChar, 50, "Department"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Vehicle_ID__", System.Data.OleDb.OleDbType.SmallInt, 0, "Vehicle ID #"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Vehicle_ID__", System.Data.OleDb.OleDbType.SmallInt, 0, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Vehicle ID #", System.Data.DataRowVersion.Original,Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Department", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Department", System.Data.DataRowVersion.Original,Nothing))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_Department1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input,False,CType(0,Byte),CType(0,Byte), "Department", System.Data.DataRowVersion.Original,Nothing))
''StatusBar1'Me.StatusBar1.Location =New System.Drawing.Point(0, 362)
Me.StatusBar1.Name = "StatusBar1"
Me.StatusBar1.Size =New System.Drawing.Size(592, 22)
Me.StatusBar1.TabIndex = 31
''TextBox2'Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("Text",Me.DataSet1, "Vehicle Info.Vehicle ID #"))
Me.TextBox2.Font =New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,CType(0,Byte))
Me.TextBox2.Location =New System.Drawing.Point(216, 24)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly =TrueMe.TextBox2.Size =New System.Drawing.Size(144, 27)
Me.TextBox2.TabIndex = 32
Me.TextBox2.Text = ""
''Label2'Me.Label2.Font =New System.Drawing.Font("Microsoft Sans Serif", 10.2!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,CType(0,Byte))
Me.Label2.Location =New System.Drawing.Point(159, 27)
Me.Label2.Name = "Label2"
Me.Label2.Size =New System.Drawing.Size(40, 24)
Me.Label2.TabIndex = 33
Me.Label2.Text = "VIN"
''Form1'Me.AutoScaleBaseSize =New System.Drawing.Size(6, 15)
Me.ClientSize =New System.Drawing.Size(592, 384)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.StatusBar1)
Me.Controls.Add(Me.btnUpdate)
Me.Controls.Add(Me.btnQuit)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.lblNavLocation)
Me.Controls.Add(Me.btnPrev)
Me.Controls.Add(Me.btnNext)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
EndSub#EndRegionPrivateSub btnPrev_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnPrev.Click
BindingContext(DataSet1, "Vehicle Info").Position = (BindingContext(DataSet1, "Vehicle Info").Position - 1)
position_Changed()
EndSubPrivateSub btnNext_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnNext.Click
BindingContext(DataSet1, "Vehicle Info").Position = (BindingContext(DataSet1, "Vehicle Info").Position + 1)
position_Changed()
EndSubPrivateSub position_Changed()
lblNavLocation.Text = (((BindingContext(DataSet1, "Vehicle Info").Position + 1).ToString + " of ") _
+ BindingContext(DataSet1, "Vehicle Info").Count.ToString)
EndSubPrivateSub btnQuit_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnQuit.Click
Application.Exit()
EndSubPrivateSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load
Adapt1.Fill(DataSet1, "Vehicle Info")
EndSubPrivateSub btnUpdate_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnUpdate.Click
TryAdapt1.Update(DataSet1, "Vehicle Info")
StatusBar1.Text = "Record Update"
CatchMsgBox("Can't Update Record", MsgBoxStyle.OKOnly)
EndTryEndSub
EndClassthe data field I am updating is a character field 50 (Char), There is also one other field I used from the Db which is an integer, Fixed format, needed because it is the Key field.
the command text for my Update command is as follows, this was created when I generated the Adapter from the toolbox.
UPDATE [Vehicle Info]
SET Department = ?, [Vehicle ID #] = ?
WHERE ([Vehicle ID #] = ?) AND (Department = ? OR
? IS NULL AND Department IS NULL)The parameter collection editor shows the parameter list as follows.
Direction OleDbType Size Srce Col. Src Ver.
0 Department Input VarWChar 50 Department Current
1 Vehicle_ID_ Input SmallInt 0 Vehicle ID # Current
2 Orig_Vehicle_ID_ Input SmallInt 0 Vehicle ID # Original
3 Orig_Department Input VarWChar 50 Department Original
4 Orig_Deparment1 Input VarWChar 50 Department Oiginal
I have written these types of Apps before and not had this problem....
This should be very simple. can anyone figure out why the data doesn't update ?
The app, runs through the update statement fine as if it worked but the data never changes.
I'd pull my hair out if I had any........
Thanks in advance
tattoo
Hi tattoo,
Your coding is updating the database!

