简体   繁体   中英

RowChanged Event Fires and Throws an Error During DataAdapter.Fill(DataTable)

At the top of my code for the form I have the following statements (and some other unrelated ones to):

Dim LastDataRow As DataRow
Public Event RowChanged As DataRowChangeEventHandler  

When my form loads I run the following line of code:

AddHandler dt.RowChanged, New DataRowChangeEventHandler(AddressOf Row_Changed)

After the form loads I have a FetchData button which connects to the database and brings the records into a datatable object. During this fill operation my Row_Changed event fires (for every record I think) and tries to run DataAdapter.Update(DataTable) (pseudocode) which ultimately fails because I haven't yet created commands. The error I get in my Row_Changed procedure is:

Update requires a valid InsertCommand when passed DataRow collection with new rows.

I hadn't even planned to use an InsertCommand at all since I will not be allowing users to insert records from this form. I do plan to allow them to delete or modify existing records, but not insert new ones.

The only way I've found to work around this problem so far is to run my AddHandler statement after filling the datatable. However, I'm trying to figure out how to rewrite my code so that I can refresh the datatable without closing out the form/application and having to open it up again. I think that's a problem for a separate discussion.

Any ideas why I'm getting the error message above?

You could RemoveHandler before calling the fill operation so Update does not get called. After the fill operation is complete, you can add the handler back again.

RemoveHandler
da.Fill(dt)
AddHandler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM