Another Newbie Question

G

Guest

I am experiencing an error when I run the following code:

Dim dsServer As New DataSet
Dim cmdServer As New SqlCommand
Dim sSelect As String = "SELECT tblServer.DNSName FROM tblServer WHERE
tblServer.ServerID = " & Integer.Parse(Request.QueryString("ID"))

cmdServer.CommandText = sSelect
cmdServer.Connection = SqlConnection1

Dim daServer As New SqlDataAdapter

daServer.SelectCommand = cmdServer
daServer.MissingSchemaAction = MissingSchemaAction.AddWithKey
daServer.Fill(dsServer)

Dim drServer As DataRow
Dim iCurrentRow As Integer

drServer = dsServer.Tables("tblServer").Rows(iCurrentRow)
txtDNSName.Text = drServer("DNSName")


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

Source Error:

Line 47: Dim drServer As DataRow
Line 48: Dim iCurrentRow As Integer
Line 49: drServer = dsServer.Tables("tblServer").Rows(iCurrentRow)
Line 50: txtDNSName.Text = drServer("DNSName")
Line 51:

Source File: c:\inetpub\wwwroot\ITApplications\ServerDetail.aspx.vb Line:
49

What am I not correctly setting. Any help would be appreciated.
 
S

sreejith.ram

it could be some thing like , this query did not return any rows

you will need to DEBUG the code, setting a break point at line 49 and
see if there are any tables in dataset dsServer , if yes then whether
that table contains any rows etc.

dsServer.Tables("tblServer").Rows(iCurrentRow)
 
S

Steve C. Orr [MVP, MCSD]

Most likely your query isn't returning any rows.
Less likely is that your DataSet contains no tables.
You can check this through debugging, or (better yet) put in error handling
code to deal with these situations.
 
G

Guest

When I run the query in Query Analyzer it brings back 1 row that I expect.
When I debug the code, the data row variable that I have named "drServer"
contains nothing. Why is the 1 record not being loaded into my dataset?
 
S

Steve C. Orr [MVP, MCSD]

Are you sure its the same query?
Perhaps the querystring is not coming through quite as you anticipated.
 
G

Guest

G'day.

Try changing this line:
drServer = dsServer.Tables("tblServer").Rows(iCurrentRow)

to this:

drServer = dsServer.Tables(0).Rows(iCurrentRow)

hth,

Cheers,

Paul
 
G

Guest

G'day Jack...

The Tables collection on the dataset object can be indexed by name or by
number. However, unless you specifically do:

myDatasetVariable.Tables.Add("MyTableName")

The new dataset will not have any table objects in its collection. Filling
the dataset from an adapter will create one, but the name will be "Table1" or
something like that. So your line of code:

drServer = dsServer.Tables("tblServer").Rows(iCurrentRow)

threw a null reference exception because there was no table with a name
"tblServer". Going in via the numeric index avoids that naming situation
altogether...

Hth,

Cheers,

Paul
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top