Solved

SQL Data Reader and Stream writer only returning 1 row

Posted on 2013-05-17
2
607 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

786 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