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

Export SQL Query to TXT

Posted on 2012-04-05
5
233 Views
Last Modified: 2012-08-13
Hello, is there a way to allow to download a SQL Query to TXT (csv) without creating the physical TXT file on the hard drive and provide a link? I use VB not C# (asp.net)

Thanks.
0
Comment
Question by:pvg1975
  • 3
  • 2
5 Comments
 

Author Comment

by:pvg1975
ID: 37812691
I will explain better. I have a SQL query that returns about 2,000 records. These records should be exported to a TXT file, but I dont want to create the file on the server's HDD, I was wondering if there's a way to allow the user to download the SQL results on a TXT file without creating such file on the hard drive. Thanks.
0
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 500 total points
ID: 37815408
Hi pvg1975,

Check this code :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Downloading....")

        Dim sch As New clsReport
        Dim fileData As New clsReport.Letter
        fileData = sch.DownloadLetter(Session("LetterID"))
        Dim ms As System.IO.MemoryStream = fileData.fil

        Dim fil As Byte() = ms.ToArray
        Dim nam As String = fileData.name
        Dim ext As String = fileData.ext
        If Not fil Is Nothing Then
            Try
                ' Response.Clear()
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + nam & "." & ext)
                ' Response.AddHeader("Content-Disposition", nam & "." & ext)
                Response.AddHeader("Content-Length", fil.Length.ToString)
                Response.ContentType = "Application/octet-stream"
                Response.Buffer = True
                Response.BinaryWrite(fil)
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try

        End If


    End Sub

Open in new window


Basically all you need to do is to build fileData string and then use Response.Write not Response.BinaryWrite.

Regards,
Chinmay.
0
 

Author Comment

by:pvg1975
ID: 37819246
Thanks, I found that code too at dotnetspark, but I couldnt make it work, I get an error at

Dim sch As New clsReport
0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37819676
Hi pvg1975,

We won't need that. You can remove it. I have given the example code as reference only based on which you can dervie your own code.

Regards,
Chinmay.
0
 

Author Comment

by:pvg1975
ID: 37819935
Yes I figured that out! Thanks :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
encoding an href string 2 24
imap mails 1 25
Function after success of Jquery/AJAX action 9 28
vb.net convert long time to mm:ss 23 12
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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