Solved

SqlDataReader doesn't Read() even though it HasRows

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now