Question Locked

Object reference not set to an instance of an object.

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

hi, I am trying to insert data into db using dataset already loaded with data from another source and here is my code;

Dim InsertSQL As String
InsertSQL = "INSERT INTO AccessToSQL (checkno , votecode, member_amount, employer_amount, paydate, basicpay, sal_scale) " & _
" VALUES (@checkno , @votecode, @member_amount, @employer_amount, @paydate, @basicpay, @sal_scale)"

Dim dr As DataRow
'here is where the error comes
For Each dr In UploadDataSet.Tables("Tablename").Rows

Dim sqlcomm As New SqlCommand(InsertSQL, DestConnection)

sqlcomm.Parameters.AddWithValue("@checkno", dr.Item("checkno").ToString)
sqlcomm.Parameters.AddWithValue("@votecode", dr("votecode").ToString)
sqlcomm.Parameters.AddWithValue("@member_amount", dr("member_amount").ToString)
sqlcomm.Parameters.AddWithValue("@employer_amount", dr("employer_amount").ToString)

and so on......
sqlcomm.ExecuteNonQuery()
Next

i get the error in the loop, I have tried to dim dr as new datarow I get another error saying "system.data.datarow.protected ....is not accessible because it is protected"

I would appreciate any help

4 replies

by tony hopkinson

Put a debug on the line and then make sure
UploadDataset isn't null, that it has a non null Tables collections and that in there there is a table called "Tablename".

One suspects not.

by jazzygodfrey

thanks tony it took me a day to figure out what the problem was, actually UploadDataset was null because of the line 'For Each dr In UploadDataSet.Tables("TableName").Rows' Tablename is a variable that carries the table that the user selected my mistake was putting the "quotes" in the tablename, you saved my day thanks again!

by tony hopkinson

wasn't me either....

Why an obect is null, can be hard to find, that it is, is indisputable.

This discussion is closed to new replies.