Solved

Export SQL Query to TXT

Posted on 2012-04-05
5
229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 66
dynamic menu in asp.net c# 11 57
ADO.NET ENTITY DATA MODEL 3 30
Reset asp.net login password 4 26
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 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