Solved

SQL Data Reader and Stream writer only returning 1 row

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 shrink a transaction log file down to a reasonable size.

896 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

12 Experts available now in Live!

Get 1:1 Help Now