Link to home
Start Free TrialLog in
Avatar of nutsch
nutschFlag for United States of America

asked on

SqlDataReader doesn't Read() even though it HasRows

Hi Experts,

I'm building a javascript string from a code-behind page with data retrieved from a sql database.

The datareader has rows, but it doesn't read. If I build the string outside of the read loop, it works (but only retrieves the first line). What am I doing wrong?

THanks for your help

  Function sFnLoadCodingLines() As String
        Dim cmdCoding As SqlCommand, drCoding As SqlDataReader, con As New SqlConnection(strcon)
        Dim sTemp As String = ""
        Dim lInvoiceID As Long

        lInvoiceID = CLng(Page.Request.QueryString("InvoiceID"))

        cmdCoding = New SqlCommand
        cmdCoding.CommandText = "SELECT BU,DEPT,AccNo,Qty,Amount,Description,EMP,VARIETAL,BRAND,STATE,PROJECT FROM OI_tblCoding WHERE isDeleted=0 AND InvoiceID=" & lInvoiceID
        cmdCoding.Connection = con
        con.Open()
        drCoding = cmdCoding.ExecuteReader
        drCoding.Read()

        If drCoding.HasRows() Then

            While drCoding.Read()
                'addRowToTable(sBU, sDPT, sAcct, dbQty, dbAmt, sDesc, sEmp, sVar, sBrand, sState, sProj)
                sTemp = sTemp & "addRowToTable(""" & drCoding("BU").ToString & _
                                """,""" & drCoding("DEPT").ToString & _
                                """,""" & drCoding("AccNo").ToString & _
                                """,""" & drCoding("Qty").ToString & _
                                """,""" & drCoding("Amount").ToString & _
                                """,""" & drCoding("Description").ToString & _
                                """,""" & drCoding("EMP").ToString & _
                                """,""" & drCoding("VARIETAL").ToString & _
                                """,""" & drCoding("BRAND").ToString & _
                                """,""" & drCoding("STATE").ToString & _
                                """,""" & drCoding("PROJECT").ToString & """);" & vbCrLf

            End While

            sFnLoadCodingLines = sTemp

        Else
            sFnLoadCodingLines = ""
        End If

        con.Close()

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Deja Anbu
Deja Anbu
Flag of Oman image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nutsch

ASKER

This is the doh! moment. I've tried to tweak that code so many times that I don't know if that line was there the whole time. Anyways, it works.

Thanks for pointing out the error of my ways,

Thomas