|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Error after removing row and then adding new row to datatableI have a button which opens a wizard that allows the user to enter a new record to a table. I have a datatable called dtItems that stores the data from a table named Items, and a dataview called dvItems that is a sorted subset of the data in dtItems. I have a datagrid called (yes, you guessed it) dgItems, which has dvItems as its datasource. When the user hits the New Item button, a new row is created with the code: rowNewItem = dtItems.NewRow I set some default values for the fields in the row, and then add the row to dtItems like so: dtItems.Rows.Add(rowNewItem) Then the wizard form opens. The user can enter data on the form, and when they hit Add Item the wizard closes and the row gets updated with the data they entered. That all works fine. The problem occurs when the user cancels the wizard and I want to remove the new row. I do this with: dtItems.Rows.Remove(rowNewItem) This seems to work, but then if the user clicks the New Item button again, I get the dreaded "object reference not set to an instance of an object" error on the dtItems.Rows.Add(rowNewItem) line, even though I have checked in the debugger to make sure that both dtItems and rowNewItem are valid object references. I have also tried setting rowNewItem to nothing before recreating it, but it makes no difference. It seems like it's trying to reuse the deleted row for some reason. Has anyone else run into this? Any help would be much appreciated. Diesel,
What does the stack trace say? It may actually be something inside the DataTable.Add method that is the problem, not dtItems and rowNewItem. You might even want to try not adding the row unless the user completes the wizard successfully. If they cancel, simply don't add it. Let me know how you go. Regards -Adam. diesel wrote: Show quoteHide quote > Ok, once again I'm at my wits' end with a VB.Net problem. > > I have a button which opens a wizard that allows the user to enter a > new record to a table. I have a datatable called dtItems that stores > the data from a table named Items, and a dataview called dvItems that > is a sorted subset of the data in dtItems. I have a datagrid called > (yes, you guessed it) dgItems, which has dvItems as its datasource. > > When the user hits the New Item button, a new row is created with the > code: > > rowNewItem = dtItems.NewRow > > I set some default values for the fields in the row, and then add the > row to dtItems like so: > > dtItems.Rows.Add(rowNewItem) > > Then the wizard form opens. The user can enter data on the form, and > when they hit Add Item the wizard closes and the row gets updated with > the data they entered. That all works fine. > > The problem occurs when the user cancels the wizard and I want to > remove the new row. I do this with: > > dtItems.Rows.Remove(rowNewItem) > > This seems to work, but then if the user clicks the New Item button > again, I get the dreaded "object reference not set to an instance of an > object" error on the dtItems.Rows.Add(rowNewItem) line, even though I > have checked in the debugger to make sure that both dtItems and > rowNewItem are valid object references. I have also tried setting > rowNewItem to nothing before recreating it, but it makes no difference. > > > It seems like it's trying to reuse the deleted row for some reason. > Has anyone else run into this? > > Any help would be much appreciated. > Diesel,
Probably is this because you declare your rowNewItem somewhere globaly. Try to avoid that as much as possible, by instance by doing this. \\\ dim rowNewItem as datarow = dtItems.NewRow //// I hope this helps, Cor Hmm. I had thought of that, but I wasn't sure how to get around it,
since I have to be able to modify the row from the wizard. I guess I'll have to create a new dataview in the wizard and somehow filter it to find the new record, rather than trying to modify rowNewItem from the wizard directly. Thanks so much for the help, Adam/Cor. I will look at the code again tonight and let you know how it goes. Thanks again, Diesel
Can't destroy Excel Process; tried Just about everything!
NullReferenceException when setting to array list Visual Basic.net How to catch a right-click My application exits after handling an ApplicationException How to copy files from desktop to web server Naming convention for objects Re: Freeze Column in DataGrid why does my app.productionversion not auto increment? Visual Basic.net and SQL Server |
|||||||||||||||||||||||