Error... Object reference not set ???

G

Guest

Can someone please tell me why I keep getting the following error! And how do
I resolve the problem???

Thanks

System.NullReferenceException: Object reference not set to an instance of an
object.

Dim src As DirectorySearcher = New
DirectorySearcher("(&(objectCategory=Person)(objectClass=user))")
createTable()
src.SearchRoot = de
src.SearchScope = SearchScope.Subtree
For Each res As SearchResult In src.FindAll
Dim topRow As DataRow = ds.Tables("users").NewRow
topRow("Name") = res.Properties("sn")(0)
ds.Tables("users").Rows.Add(topRow)
Next
DataGrid1.DataSource = ds.Tables("users")
DataGrid1.DataBind()
 
J

Joseph Byrns

Where does the error occur? Looking at the code either de is nothing or
Table("users") is nothing.
 
G

Guest

sn might equal nothing in some cases!

Do I need to put in some kind of error capture statement???

If so how would I write it???

Perhaps something LIKE this???

Thanks for the response!

Dim src As DirectorySearcher = New
DirectorySearcher("(&(objectCategory=Person)(objectClass=user))")
createTable()
src.SearchRoot = de
src.SearchScope = SearchScope.Subtree
For Each res As SearchResult In src.FindAll
If res.Properties("sn")(0) is Nothing then
response.write ("Null")
Else
Dim topRow As DataRow = ds.Tables("users").NewRow
topRow("Name") = res.Properties("sn")(0)
ds.Tables("users").Rows.Add(topRow)
End If
Next
DataGrid1.DataSource = ds.Tables("users")
DataGrid1.DataBind()
 
J

Joseph Byrns

That might well give you an out of range type exception aswell, you should
probably try something like:

If res.Properties("sn").Count > 0 Then
If res.Properties("sn")(0) is Nothing then
response.write ("Null")
Else
Dim topRow As DataRow = ds.Tables("users").NewRow
topRow("Name") = res.Properties("sn")(0)
ds.Tables("users").Rows.Add(topRow)
End If
endif
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top