D
diesel
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.
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.