473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Error after removing row and then adding new row to datatable

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
3 3877
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
6
by: Mark | last post by:
I'm trying to add a table to a dataset but get the error: "A DataTable named 'BordDates0040' already belongs to this DataSet." Code: Dim dtBordDates As DataTable dtBordDates =...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
1
by: Dagan | last post by:
Hello I have built a DataGridView that its data source is binded to a DataTable after adding it 200 rows I try to remove the first row and the program stuck, What could it be???? Thanks.
4
by: Steve | last post by:
hi all, i'm using DHTML to generate a dynamic table with adding removing and sorting rows the thing is after a row has been added at a position that i specify, if i try to delete it i get this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.