?
Solved

System.NullReferenceException Was Unhandled By User Code When Looping Through DataSet

Posted on 2006-05-01
8
Medium Priority
?
5,122 Views
Last Modified: 2007-12-19
Hi,

I'm using ASP.NET 2 with VB.NET and SQL Server 2005.  The following code produces the error System.NullReferenceException was unhandled by user code:

If .Rows.Count > 0 Then

I've tested my stored procedure called "spSelTest" with the SqlDataReader and it works fine, but I'm having  trouble using my stored procedure to loop through the DataSet.  My entire code is as follows:


Private Sub TestDataSetLoop()
        Dim sSQL As String

        'Set up sql string and connection
        sSQL = "spSelTest"

        'Use clsDatabase class for connection string
        sCon = oCon.SqlConString
        Dim Con As SqlConnection = New SqlConnection(sCon)

        Dim da As New SqlDataAdapter(sSQL, sCon)
        Dim ds As New DataSet
        da.Fill(ds)

        Dim i As Integer
        Dim iID As Integer
        Dim sName As String
        Dim iFiller As Integer
        iFiller = 77

        With ds.Tables("fr_GeneratorInfo")
            If .Rows.Count > 0 Then
                For i = 0 To .Rows.Count - 1
                    iID = IIf(IsDBNull(.Rows(i).Item("pkcGenerator")), iFiller, .Rows(i).Item("pkcGenerator"))
                    sName = IIf(IsDBNull(.Rows(i).Item("sName")), "None", .Rows(i).Item("sName"))
                 Next
            End If
        End With

Any Suggestions?
Thanks,
Denise
0
Comment
Question by:DeniseGoodheart
8 Comments
 
LVL 4

Expert Comment

by:VIkasumit
ID: 16575653
Hi,

can you please let me know on which line it produce error.

And as you said in loop, which iteration it gives error, first or last or intermediate which one...

VIkasumit
0
 

Author Comment

by:DeniseGoodheart
ID: 16575756
Hi VIkasumit,

The following LINE of code produces the error:

If .Rows.Count > 0 Then

It never makes it pass the If .Rows.Count > 0 Then line of code.

Thanks,
Denise

0
 

Author Comment

by:DeniseGoodheart
ID: 16575829
...The error message also had the following.

  System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."

Thanks,
Denise
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Assisted Solution

by:sukumar_diya
sukumar_diya earned 80 total points
ID: 16575980
Dude,
Check for
if .Rows is nothing then
....

I hope the Rows object itself is nothing thats why it is t hrowing error... or may be the table itself is nothing ...

Suk
0
 
LVL 9

Accepted Solution

by:
tolgaong earned 1500 total points
ID: 16576363
put a break point to the line
With ds.Tables("fr_GeneratorInfo")

and check the ds.Tables(0).tablename from the watch
you may write the table name wrong.

also, you don't need the
 If .Rows.Count > 0 Then
check. because
    For i = 0 To .Rows.Count - 1 is enough for the case that you have 0 rows.
0
 
LVL 4

Assisted Solution

by:VIkasumit
VIkasumit earned 420 total points
ID: 16576628
Hi,

to my experince, I often found it get case sensitive in some case, Try to print all the tables in DS and check if it has same name as yours, VB is case insensitive but Dataset may not be, its XML based and XML files are case sensitive in nature.

Its a simple Rule for ASP.Net Developer that when ever use XML based component write case sensistive code only...

Also to work you can use Try catch block or Is nothing check but, to me it seems your table is filling wrong way as there must be some count for table , if you successfully runs with datareader.

Try Datatable instead of Dataset and check. Surly your DataSet is not filling properly or you have null data in some of your column in table. Dataset some time fails for me if that happens

VIkasumit
0
 

Author Comment

by:DeniseGoodheart
ID: 16579662
Hi All,

I changed my code from With ds.Tables("fr_GeneratorInfo") to With ds.Tables(0) and it worked fine.

Many Thanks,
Denise

0
 
LVL 4

Expert Comment

by:VIkasumit
ID: 16583374
Hi,

Its good to see , but as I said, your table wasn't setting there ..

you can alternatively do this .

da.Fill(ds ,"fr_GeneratorInfo")

while filling the table to give a name. And this is preferred over tables(0) if you have two or more table to parse

VIkasumit
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question