Solved

SqlDataReader doesn't Read() even though it HasRows

Posted on 2014-02-11
3
403 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now