Friday, March 16, 2012

Object reference not set to an instance of an object.

G'day,

I'm feeling really ignorant but can't get past this error.
I don't seem to be able to fix without getting other syntax errors.

I'm copping this at the line marked with *

Sub getImg(ByVal FileID As Integer, ByVal ds As DataSet)
Dim dr As DataRow
Dim dt As DataTable

dt = ds.Tables("Images")

For Each dr In dt.Rows '*
If dr("fileID") = FileID Then

'set up some stuff

Exit For
End If
Next
End Sub

Incidently is there a slicker way of finding a record in a dataset?

Thanks,

GeoffI think you need to use the new keyword when u declare the datarow and the datatable.

Try


dim dr as new DataRow
dim dt as new DataTable


I could make dt a new instance but can't do the same for dr.
I get a message when mousing over 'dim dr as new DataRow' saying

'System.Data.DataRow.Protected Sub New(builder as System.Data.DataRowBuilder)' is not accessible in this context because it is 'Protected'.

Just when I was becoming confident I get something like this. I've no idea what's going on.

Thanks for your suggestion though!

Geoff
you do not need the new keyword for either statement as you arent creating new tables or rows. your just referencing other tables/rows.

my guess is that perhaps ds.Tables("Images") returned nothing.
Here is a link that will help you create DataTable and DataRows.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatatableclasstopic.asp

0 comments:

Post a Comment