Solved

Export SQL Query to TXT

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Text in tooltip bound field bridview 3 30
send mail from app.net 8 28
Duplicate a row 2 33
Asp.net mvc foreach 3 6
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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

18 Experts available now in Live!

Get 1:1 Help Now