Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
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
Updated 27-Feb-12 1:37am
v2
Comments
Nandakishore G N 27-Feb-12 7:31am    
change..the language...to vb...many will answer....it is not c#....

You are trying to access a member of an object that is null or not set.
Read more about this error here[^].
 
Share this answer
 
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).
 
Share this answer
 
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.
 
Share this answer
 
Comments
kishore sharma 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900