473,698 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ad d(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.Re move(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.Ad d(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 3888
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.Ad d(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.Re move(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.Ad d(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
4257
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
17
7456
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 Const xyzcErrNumBase As Long = 500 Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1 Public Const xyzcErrTxtHeyDontDoThat = "Hey! Don't do that!"
6
4748
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 appreciated. Thanks in advance
2
5783
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 new record to a subform. If I close msgbox I can then successfully add new record. Have looked on MS Technet and tried their 2 suggestions:- 1)LinkChildFields property contains the name of a form property - this isn't the case linkChildFields...
0
4798
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 manually inserted a row in the table bound to the Combo box. The InsertAt Method of adding a row just does not work. Hope this helps anyone with this problem. john
6
2236
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 = LoadTable(strDBPath & strDBName, strTableName, strSQL) Dim ds2 As DataSet = New DataSet()
6
6106
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 more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
1
1049
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
2872
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 error ! here's a chunk of code <script language='javascript' type='text/css' > function removeElement(divNum,arrayCode,elementId) {
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.