I'm pretty new to VB.NET so I'm trying to start simple:
I have a single table database in SQL SERVER 2005 (not express) that I am trying to update from a VB 2008 EXPRESS app. The database is very simple and consists of one table with one column, Taskname, which is the primary key.
The form I have moves through the records in the dataset (Taskname values) and allows the user to edit the name of the task if needed. If it changes, it is supposed to connect to the data source and update the name of the task in the database. The problem I am having is that it is updating the data source like this:
1st edit: value is "Reboot" in dataset and I update it to "RebootTEST", it updates the dataset value to "RebootTEST" but does not update the data source
2nd edit: value is "RebootTEST" in dataset and I update it to "RebootTEST2", now it updates the dataset value to "RebootTEST2" and updates the data source to "RebootTEST"
3rd edit: value is "RebootTEST2" in dataset and I update it to "RebootTEST2EDIT", it updates the dataset value to "RebootTEST2EDIT", but does not update the data source
4th edit: value is "RebootTEST2EDIT" and I update it to "RebootTEST2EDIT2", I receive an error message "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records"
I know the error is generated because the source value doesn't match the dataset value, but why is this if I'm telling it to update the data source after each change in the dataset?
Here is the code for the form load event:
Private Sub frmTaskDEL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load con.ConnectionString() = My.Settings.SharepointTitle con.Open() Taskadapter.FillSchema(TASKSET, SchemaType.Mapped, "TASKS") Taskadapter.Fill(TASKSET, "TASKS") BSTASK.DataSource = TASKSET.Tables("TASKS") Taskreader = TaskSelectcommand.ExecuteReader() Do While Taskreader.Read() Dim a As Integer = 0 ComboBox1.Items.Add(Taskreader(a)) a = a + 1 Loop Taskreader.Close() con.Close() TextBox1.DataBindings.Add(New Binding("Text", BSTASK, "Taskname")) 'since ComboBox1 is a lookup for TextBox1, DO NOT BIND! 'ComboBox1.DataBindings.Add(New Binding("Text", BSTASK, "Taskname")) End Sub
Here is the code for the button that updates the data source (or should anyway):
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim messresult As DialogResult Dim DSUVal As Integer DSUVal = 0 Try If TextBox1.Modified = True Then messresult = MessageBox.Show("Save this record?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If messresult = DialogResult.Yes Then DSUVal = 1 ElseIf messresult = DialogResult.No Then TextBox1.Undo() Else Exit Sub End If End If If DSUVal = 1 Then con.Open() Taskadapter = TaskCB.DataAdapter() Taskadapter.UpdateCommand = TaskCB.GetUpdateCommand() Dim numrowsaffected As Integer numrowsaffected = Taskadapter.Update(TASKSET, "TASKS") MsgBox(numrowsaffected) con.Close() End If If Me.BSTASK.Position <> 0 Then BSTASK.MovePrevious() Else MsgBox("This is the first record", MsgBoxStyle.Exclamation) Exit Sub End If Catch ex As Exception MsgBox(Err.Description) End Try End Sub
BSTASK is a binding source and TaskCB is a commandbuilder.
Any idea where I've gone off track? Like I said I'm pretty new at this. Any help you can give would be appreciated. Thanks.