Hi all. I have been playing around with a TableAdapter that has two joined tables and one added column with the expression parameter set. A Bindingsource is used to connect this TableAdapter to a DataGridView. Everything displays correctly and as expected.
I have generated an OleDB UPDATE/DELETE/INSERT command that is called (as shown below) to update one of the joined tables. When the code runs, feedback through msgboxes indicate that there are NO changes being made to the Datatable (TableAdapter). I have verified that the data input through the DataGridView is being written to the DataTable.
So my question is: Will a TableAdapter set up like this capture when changes are made or is something special needed to capture changes? If yes, is there something that I am missing in the UPDATE code?The code runs without throwing any errors. My connections is to an ACCESS DB.
Code:Private Sub btn_SaveWeightData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_SaveWeightData.Click Me.Validate() TblWeightDataBindingSource.EndEdit() Call UpdateWeightTable() End SubCode:Public Sub UpdateWeightTable() Dim StrCon As String = Connectionstring() 'Get connection string Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection(StrCon) Dim dt As DataTable = frmMainScoreBoard.ScoreboardDataSet.tbl_WeightData Dim dataAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter() dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey Dim parameter As OleDb.OleDbParameter Dim command As OleDb.OleDbCommand 'Generate Insertcommand statement command = New OleDb.OleDbCommand("INSERT INTO tbl_WeightData (StudentID, LastWeighinDate, LastWeighinWeight) " & _ "VALUES (@StudentID, @LastWeighinDate, @LastWeighinWeight)", connection) command.Parameters.Add("@StudentID", OleDb.OleDbType.VarChar, 200, "StudentID") command.Parameters.Add("@LastWeighinDate", OleDb.OleDbType.VarChar, 200, "LastWeighinDate") command.Parameters.Add("@LastWeighinWeight", OleDb.OleDbType.SmallInt, 5, "LastWeighinWeight") dataAdapter.InsertCommand = command ' Create the DeleteCommand command = New OleDb.OleDbCommand("DELETE * FROM tbl_WeightData WHERE Index = @Index", connection) parameter = command.Parameters.Add("@Index", OleDb.OleDbType.Integer, 3, "Index") parameter.SourceVersion = DataRowVersion.Original dataAdapter.DeleteCommand = command 'Generate the first UpdateCommand Dim updateSQL As String = "UPDATE tbl_WeightData SET " & _ "StudentID = @StudentID, " & _ "LastWeighinDate = @LastWeighinDate, " & _ "LastWeighinWeight = @LastWeighinWeight WHERE Index = @Index" command = New OleDb.OleDbCommand(updateSQL, connection) command.Parameters.Add("@StudentID", OleDb.OleDbType.VarChar, 200, "StudentID") command.Parameters.Add("@LastWeighinDate", OleDb.OleDbType.VarChar, 200, "LastWeighinDate") command.Parameters.Add("@LastWeighinWeight", OleDb.OleDbType.SmallInt, 5, "LastWeighinWeight") parameter = command.Parameters.Add("@Index", OleDb.OleDbType.Integer, 3, "Index") parameter.SourceVersion = DataRowVersion.Current dataAdapter.UpdateCommand = command 'Update Tbl_MatchData in database dataAdapter.AcceptChangesDuringUpdate = False If dt.GetChanges() Is Nothing Then 'Check to see if there are any changes to the dt before update MessageBox.Show("Before Green UPdate-There are no changes to save.") Else MessageBox.Show("Before Green UPdate The are changes to be saved.") End If Dim returnvalue As Integer = dataAdapter.Update(dt) If dt.GetChanges() Is Nothing Then 'Check to see if there are any changes to the dt after update MessageBox.Show("After Green UPdate-There are no changes to save.") Else MessageBox.Show("After Green UPdate The are " & returnvalue & " changes to be saved.") End If dt.AcceptChanges() connection.Close() dt.Dispose() End Sub




The code runs without throwing any errors. My connections is to an ACCESS DB.
Reply With Quote