By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Error after removing row and then adding new row to datatable

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:
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.

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.