Click here to Skip to main content
11,411,840 members (66,938 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Windows Application
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



regards
......
Posted 27-Feb-12 2:27am
Edited 27-Feb-12 2:37am
v2
Comments
Nandakishorerao at 27-Feb-12 7:31am
   
change..the language...to vb...many will answer....it is not c#....
Rate this: bad
good
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[^].
  Permalink  
Rate this: bad
good
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:

ds.Tables("AddressBook").Rows.Add(dsNewRow)

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).
  Permalink  
Rate this: bad
good
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.
  Permalink  
Comments
kishore sharma at 27-Feb-12 8:37am
   
hello,
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
0 OriginalGriff 268
1 Sergey Alexandrovich Kryukov 200
2 Sascha Lefévre 170
3 ProgramFOX 130
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,955
1 OriginalGriff 7,158
2 Maciej Los 3,480
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 27 Feb 2012
Copyright © CodeProject, 1999-2015
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