Solved

SqlDataReader doesn't Read() even though it HasRows

Posted on 2014-02-11
3
409 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 400 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 100 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 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