Question : System.NullReferenceExcept<wbr />ion Was Unhandled By User Code When Looping Through DataSet

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

Answer : System.NullReferenceExcept<wbr />ion Was Unhandled By User Code When Looping Through DataSet

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.
Random Solutions  
 
programming4us programming4us