Rows added from another form but not update in datatable

rajdh75

Active member
Joined
Mar 30, 2020
Messages
29
Programming Experience
Beginner
I have a form named FrmInvoice. It has a datagridview witch populate data from following code on form load event.
VB.NET:
 Try
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Access\Sale.accdb")
            'Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM TblSale", con)
            con.Open()
            myDA = New OleDbDataAdapter(cmd)
            'Automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object   
            Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
            myDataSet = New DataSet()
            myDA.Fill(myDataSet, "TblSale")
            DataGridView1.DataSource = myDataSet.Tables("TblSale").DefaultView
            DataGridView1.ColumnHeadersVisible = False
            con.Close()
            con = Nothing

        Catch ex As Exception
            MsgBox("Error", ex.Message)
        End Try
The Button Save has following code
VB.NET:
 Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("TblSale"))
        Me.myDataSet.AcceptChanges()
        MessageBox.Show("Updated.")
        Me.Close()
I have a another form named as FrmAdd which have text boxes as TxtItems, TxtQty, TxtRate, TxtAmount and a Button OK. For adding rows to datagridview to FrmInvoice. the code is
VB.NET:
Dim dt As DataTable = CType(FrmInvoice.DataGridView1.DataSource, DataView).Table

        Dim drToAdd As DataRow = dt.NewRow
        drToAdd("Items") = TxtItems.Text
        drToAdd("Qty") = Convert.ToInt32(TxtQty.Text)
        drToAdd("Rate") = Convert.ToDecimal(TxtRate.Text)
        drToAdd("Amount") = Convert.ToDecimal(TxtAmount.Text)
        dt.Rows.Add(drToAdd)
        dt.AcceptChanges()
        TxtItems.Focus()
The row is added to the datagridview but when Save Button is pressed in FrmInvoice Form, the message is shown as updated, no exception is thrown. When I check by loading form again, it is not added to the database.

Any Alternate code for this ?

Thanks

-
 
When you call AcceptChanges is sets row state to Unchanged, so there is nothing to update to db later.
 
So many people seem to think that AcceptChanges actually saves changes. It doesn't. It basically tells the DataTable that the changes have been saved so it doesn't need to track them any more. The way you actually save changes is by calling Update on a data adapter - preferably the same data adapter that you called Fill on to retrieve the data in the first place. Not surprisingly, Update calls AcceptChanges implicitly by default.
 
Back
Top