Object reference not set

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top