Object reference not set

G

Guest

I keep getting the following error! I'm sure it is a really simple problem
but just can't see what is causing it!

The error occors on the FOR line!

I would really appritiate any advice.

Thanks



...:: ERROR
Object reference not set to an instance of an object.

...:: CODE
Function GetSelectedIndex(ByVal OID As String) As Integer
Dim iLoop As Integer
Dim dt As DataTable = ddlDataSet.Tables("Offices")
For iLoop = 0 To dt.Rows.Count - 1
If Int32.Parse(OID) = Int32.Parse(dt.Rows(iLoop)("officeID")) Then
Return iLoop
End If
Next iLoop

End Function
 
K

Karl Seguin

dt is null/nothing . ddDAtaSet.Tables("Offices") mustn't be a valid name
for a table. Consider simply trying by # for now, like
ddDataSet.Tables(0) and see if that works.

Karl
 
G

Guest

As far as I can see right now it looks fine!

Here is the code that creates the dataset! What else could be wrong???

Thanks

Function GetCategories() As DataSet
'Populate the ddlDataSet
Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter("SelectOffice",
Myconn)
myDataAdapter.Fill(ddlDataSet, "Offices")
Return ddlDataSet
End Function
 
K

Karl Seguin

Like I said, try to access if via ddlDataSet.Tables(0)

also try to check the value of ddlDataSet.Tables(0).TableName which might
help you see the problem.

I assume in your GetSelectedIndex function ddlDataSet is some member-level
variable, since it isn't passed into the function...

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top