Solved

SQL Data Reader and Stream writer only returning 1 row

Posted on 2013-05-17
2
615 Views
Last Modified: 2013-05-17
I have the following code that is pulling data from my sql table and putting it in a cvs file.

I can only get it working for the first row. That table has 145 rows in it.

    Protected Sub cmdDownload_Click() Handles cmdDownload.Click

        Dim constr As String = ConfigurationManager.ConnectionStrings("baminterchangerConnectionString").ConnectionString
        Using sqlConnectionObject As New SqlConnection(constr)

            Dim sqlQueryCommand As New SqlCommand("SELECT * FROM [dbo].[imports]", sqlConnectionObject)
            Dim sqlResultSet As SqlDataReader

            sqlConnectionObject.Open()
            sqlResultSet = sqlQueryCommand.ExecuteReader()

            Dim savedFiles As String
            Dim filename As String = (GetCurrent.Name)
            savedFiles = Path.GetFileName(filename & ".csv")

            Dim Excel As String = Server.MapPath("temp\" & savedFiles)

            Using streamWriterObject As New StreamWriter(Excel)
                    Do While sqlResultSet.Read
                        sqlResultSet.GetSqlValue(0)
                        sqlResultSet.GetSqlValue(1)
                        'Write a line of stream of each value of the columns in the table to the stream object
                        streamWriterObject.WriteLine(sqlResultSet(0).ToString() & "," & sqlResultSet(1).ToString())
                        sqlResultSet.NextResult()
                    Loop
                    MessageBox("Successfully Uploaded CSV File")
            End Using

        End Using


    End Sub

End Class

Open in new window

0
Comment
Question by:bignadad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39176507
Since you only have one resultset the following line needs to be commented out:
sqlResultSet.NextResult()

The Read method skips to the next row.
0
 
LVL 2

Author Closing Comment

by:bignadad
ID: 39176510
Worked perfect!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

687 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