Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15463

VS 2019 DataRowState.Detached

$
0
0
I have a form that is bound to a datatable. This form is used to add, delete, and modify rows in the datatable and also to perform some other housekeeping functions. These functions need the autonumber (primary key) value from the current row.

Normally the user would add one or more rows, via the bindingnavigator's Add button, then click the Save button to persist all changes to the underlying MS Access database table by calling the dataadapter.update method. This also updates the RecID column of all added row(s) with the value of the autonumber (primary key field) from the MS Access database table.

There is one case where I need to save (persist) only the bindingsource's current row to the underlying MS Access database because I need the row's autonumber column value before performing a housekeeping function on the row. I want to ignore any other changed rows at this point (their changes will be dealt with elsewhere).

In this case, the user adds a new row and **before clicking the Save button**, may click one of the housekeeping buttons. I want to save (persist) only that row and not any other rows that have changes.

I am having difficulty with this one case and need advice. Here is my code for when the user clicks a housekeeping button.

Code:

Select Case CType(bsMain.Current, DataRowView).Row.RowState
    Case DataRowState.Added, DataRowState.Detached
        strMSG = "This is a new vendor." & vbCrLf & vbCrLf &
        "Before you can assign vendor types to a new vendor you must first save the vendor." & vbCrLf & vbCrLf &
        "Do you want to save this vendor now?"
        If MessageBox.Show(strMSG, strMethodName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Validate()
            CType(bsMain.Current, DataRowView).Row.EndEdit()
            rows(0) = CType(bsMain.Current, DataRowView).Row
            daVendors.Update(rows)
            rows(0).AcceptChanges()
            bsMain.ResetBindings(False)
            lngVendorID = CLng(rows(0).Item("RecID"))
        End If
End Select

When the user adds a new row, its DataRowState is Detached and the user is prompted to save the row before proceeding.

Calling the CType(bsMain.Current, DataRowView).Row.EndEdit method does not change the DataRowState from from Detached to Added.

When the daVendors.Update method is called, the following error is raised.

Name:  2021-04-28_4-50-23.jpg
Views: 45
Size:  36.0 KB

What am I doing wrong? Why does the DataRowState remain Detached?
Attached Images
 

Viewing all articles
Browse latest Browse all 15463

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>