Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi All

When using the this code


<%@dotnet.itags.org. Page Language="VB" %>
<%@dotnet.itags.org. import Namespace="System.Data" %>
<%@dotnet.itags.org. import Namespace="System.Data.SqlClient" %>
<script runat="server"
Sub Button2_Click(sender As Object, e As EventArgs)

Dim conn As new sqlconnection ("server='sql2.redstation.co.uk'; user id='2128779.150'; password='CLT42876'; database='2128779.150'")

dim dbcommanda as new sqlcommand("SELECT * from tblReg", conn)

dim myDataSet as New Dataset()

dim daprods as new sqldataadapter(dbcommanda)

daprods.fill(myDataSet,"Tony")

if myDataSet.Tables("Details").rows.count = 0 then

response.write("Empty")
end if

conn.close()

I get this error

Server Error in '/' Application.
------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Can Anyone tell me what i am doing wrong?

rgs

TonyPlease specify what line the error occurs on. That will make it lots easier to diagnose.
At first glance, I think the problem might be that you are trying to reference a datatable in the dataset that doesn't exist - Details. When you filled the dataset...

daprods.fill(myDataSet,"Tony")

.. a datatable was created named Tony. The data would then have been loaded into that table. Next, you try to get info about a datatable named Details...

if myDataSet.Tables("Details").rows.count = 0 then

.. which doesn't exist. Two things you can do in order to test this theory:

1. don't provide any datatable name during fill():
daprods.fill(myDataSet).
Then reference the datatable by its ordinal value:
if myDataSet.Tables(0).rows.count = 0 then

2. Try change Tony to Details: daprods.fill(myDataSet,"Details")

Hopefully this helps or at least points you in the right direction.

Let me know if this works :-]
The problem appears to be that in your Fill call, you're telling it to fill a table called Tony. In your check for an empty return, you're checking against a table called Details. There is no Details table in your dataset, so you're trying to reference an object that doesn't exist, which is why you're getting that error.
JBR thanks

ProgUser....thanks for the tips...worked a treat

0 comments:

Post a Comment