Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SqlDataReader doesn't Read() even though it HasRows

Posted on 2014-02-11
3
Medium Priority
?
423 Views
Last Modified: 2014-02-12
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

0
Comment
Question by:nutsch
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1600 total points
ID: 39852175
You read the  first line before checking if the DataReader has rows, and you do nothing with the result of that Read,  so you lose it.

If your query return only one line, then While is skipped even if there are Rows, because the only one that was there was already Read.
0
 
LVL 14

Assisted Solution

by:dejaanbu
dejaanbu earned 400 total points
ID: 39853083
0
 
LVL 39

Author Closing Comment

by:nutsch
ID: 39853788
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

773 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