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
Solved

SQL Data Reader and Stream writer only returning 1 row

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

839 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