Click here to Skip to main content
12,951,823 members (68,127 online)
Rate this:
Please Sign up or sign in to vote.
I've a bit problem with my coding. What i m trying to do is to add a row in a table, but i m having errors.
When i use
Dim row as Datarow
row = ds.Tables("Admin").Rows.Add()
I m having error "Object reference not set to an instance of an object."
any idea please help me

Posted 27-Feb-12 1:27am
Updated 27-Feb-12 1:37am
Nandakishorerao 27-Feb-12 7:31am
change..the vb...many will is not c#....
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are trying to access a member of an object that is null or not set.
Read more about this error here[^].
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Dim dsNewRow As DataRow

If you want to add a new row to your DataSet, you need a DataRow object. This line just sets up a variable called dsNewRow. The type of variable is a DataRow.

To create the new DataRow object, this line comes next:

dsNewRow = ds.Tables("AddressBook").NewRow()

We're just saying, "Create a New Row object in the AddressBook DataSet, and store this in the variable called dsNewRow." As you can see, NewRow() is a method of ds.Tables. Use this method to add rows to your DataSet.

The actual values we want to store in the rows are coming from the textboxes. So we have these two lines:

dsNewRow.Item("FirstName") = txtFirstName.Text
dsNewRow.Item("Surname") = txtSurname.Text

The dsNewRow object we created has a Property called Item. This is like the Item property you used earlier. It represents a column in your DataSet. We could have said this instead:

dsNewRow.Item(1) = txtFirstName.Text
dsNewRow.Item(2) = txtSurname.Text

The Item property is now using the index number of the DataSet columns, rather than the names. The results is the same, though: to store new values in these properties. We're storing the text from the textboxes to our new Row.

We now only need to call the Method that actually adds the Row to the DataSet:


To add the Row, you use the Add method of the Rows property of the DataSet. In between the round brackets, you need the name of your DataRow (the variable dsNewRow, in our case).
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

In your debugger, have a look at your variable ds. Is it valid, does it contain a reference to a dataset or is it null?

Also, you are trying to access a table named 'Admin' within the DataSet. If the dataset is valid, does it contain a table named Admin? If not, this would return null.

Check both of these out in your debugger, you should be able to determine where the problem is.
kishore sharma 27-Feb-12 8:37am
if i look at this code
Dim row as Datarow
row = ds.Tables("Admin").Rows.Add()
it doesnt seems that you are adding row to table
wht you are doing
1st line-> Created datarow
2nd line->right side you have table to which you are tring to add & your row is on left side . i think its not currect.
better you explain wht you need you will get sollution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,229
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 27 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100