The table in question has 2 rows and I am making changes to just one of them (via the dataset). The dataset (which contains both rows) is altered as follows:
"If row("Centre_Name").ToString = lstCentres.SelectedItem.ToString" determines which row has been edited.Code:For Each row As DataRow In ds_Centres.Tables(0).Rows If row("Centre_Name").ToString = lstCentres.SelectedItem.ToString Then row("Centre_Name") = txtCentreName.Text row("Monday_Class") = chkClass_Days0.Checked row("Tuesday_Class") = chkClass_Days1.Checked row("Wednesday_Class") = chkClass_Days2.Checked row("Thursday_Class") = chkClass_Days3.Checked row("Friday_Class") = chkClass_Days4.Checked row("Saturday_Class") = chkClass_Days5.Checked row("Sunday_Class") = chkClass_Days6.Checked row("Correspondence") = chkClass_Days7.Checked End If
Now this code attempts to update the table:
What is going wrong is that the 2 rows in the table become 2 copies of the row which had changes made to it. The row which was unchanged simply disappears!Code:Dim Conn As OleDbConnection Dim SQL As String Dim ConnectionString As String SQL = "SELECT * FROM tblCentre_Details Order by ID" ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & My.Settings.AppPath & "\" & ManagerName & "_FCLManager2011_Centres.mdb" Conn = New OleDbConnection(ConnectionString) Dim da = New OleDbDataAdapter(SQL, Conn) da.UpdateCommand = New OleDbCommand("UPDATE tblCentre_Details SET Centre_Name = @Centre_Name, Monday_Class = @Monday_Class, Tuesday_Class = @Tuesday_Class, Wednesday_Class = @Wednesday_Class, Thursday_Class = @Thursday_Class, Friday_Class = @Friday_Class, Saturday_Class = @Saturday_Class, Sunday_Class = @Sunday_Class, Correspondence = @Correspondence") da.UpdateCommand.Connection = Conn da.UpdateCommand.Parameters.Add("@Centre_Name", OleDbType.VarChar, 50, "Centre_Name") da.UpdateCommand.Parameters.Add("@Monday_Class", OleDbType.Boolean, 1, "Monday_Class") da.UpdateCommand.Parameters.Add("@Tuesday_Class", OleDbType.Boolean, 1, "Tuesday_Class") da.UpdateCommand.Parameters.Add("@Wednesday_Class", OleDbType.Boolean, 1, "Wednesday_Class") da.UpdateCommand.Parameters.Add("@Thursday_Class", OleDbType.Boolean, 1, "Thursday_Class") da.UpdateCommand.Parameters.Add("@Friday_Class", OleDbType.Boolean, 1, "Friday_Class") da.UpdateCommand.Parameters.Add("@Saturday_Class", OleDbType.Boolean, 1, "Saturday_Class") da.UpdateCommand.Parameters.Add("@Sunday_Class", OleDbType.Boolean, 1, "Sunday_Class") da.UpdateCommand.Parameters.Add("@Correspondence", OleDbType.Boolean, 1, "Correspondence") Try Conn.Open() da.Update(ds_Centres) ds_Centres.AcceptChanges() da.Dispose() Conn.Close() Catch ex As Exception MsgBox("Cannot open connection ! ") End Try
What am I doing wrong, please?




Reply With Quote