473,699 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create csv file in asp.net

J

hi, all,
I have an asp.net page which let user view data from database (using dataset),
but now, how can I create a CSV file in asp.net then let user save to their
local machine?
Thanks in advance.
J.
Nov 18 '05 #1
4 2219

If you want to keep the files on your server:

1. Loop through the rows, columns in a dataset and write them out to a file,
give it some kind of unique name
2. http://www.freevbcode.com/ShowCode.a...583&NoBox=True


"J" wrote:

hi, all,
I have an asp.net page which let user view data from database (using dataset),
but now, how can I create a CSV file in asp.net then let user save to their
local machine?
Thanks in advance.
J.

Nov 18 '05 #2
Hi J,

Here's some code that should get you going. It grabs data from the SQL
database as a datareader and pushes it out as a CSV. No storage of the file
required. You should see Excel open with the data.

Does this help?

Ken
Microsoft MVP [ASP.NET]
Imports System.Data.Sql Client
Imports System.IO
Public Class csv
Inherits System.Web.UI.P age
Protected WithEvents Button1 As _
System.Web.UI.W ebControls.Butt on
Protected WithEvents SqlConnection1 As _
System.Data.Sql Client.SqlConne ction
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> _
Private Sub InitializeCompo nent()
Me.SqlConnectio n1 = _
New System.Data.Sql Client.SqlConne ction
'
'SqlConnection1
'
Me.SqlConnectio n1.ConnectionSt ring = _
"data source=P4320;in itial catalog=" & _
"Northwind;pass word="""";persi st security info=Tru" & _
"e;user id=sa;workstati on id=P4320;packet size=4096"
End Sub
Private Sub Page_Init _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Init
'CODEGEN: This method call is
'required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click
'Set the appropriate ContentType.
Dim filename As String = "orderdetails.c sv"
Dim myCommand As New SqlCommand _
("select * from [order details] ", SqlConnection1)
myCommand.Conne ction.Open()
Dim myReader As SqlDataReader = _
myCommand.Execu teReader _
(CommandBehavio r.CloseConnecti on)
Dim i As Integer
Dim sb As New System.Text.Str ingBuilder
For i = 0 To myReader.FieldC ount - 1
If i < (myReader.Field Count - 1) Then
sb.Append(Chr(3 4) & myReader.GetNam e(i) & _
Chr(34) & ",")
Else
sb.Append(Chr(3 4) & myReader.GetNam e(i) & _
Chr(34) & vbCrLf)
End If
Next
While myReader.Read()
For i = 0 To myReader.FieldC ount - 1
If i < (myReader.Field Count - 1) Then
sb.Append(Chr(3 4) & _
myReader.GetVal ue(i).ToString & Chr(34) & ",")
Else
sb.Append(Chr(3 4) & _
myReader.GetVal ue(i).ToString & Chr(34) & vbCrLf)
End If
Next
End While
myReader.Close( )
SqlConnection1. Close()
Response.Conten tType = "Applicatio n/x-msexcel"
Response.AddHea der _
("content-disposition", "attachment ; filename=""" & _
filename & """")
'Write the file directly to the HTTP output stream.
Response.Write( sb.ToString)
Response.End()
End Sub
End Class

<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="csv .aspx.vb"
Inherits="p4320 work.csv"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>csv</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "FlowLayout ">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Launch"></asp:Button>
</form>
</body>
</HTML>

"J" <j@NoSpam.org > wrote in message
news:9m******** *************** *********@4ax.c om...

hi, all,
I have an asp.net page which let user view data from database (using
dataset),
but now, how can I create a CSV file in asp.net then let user save to
their
local machine?
Thanks in advance.
J.


Nov 18 '05 #3
J
Hi, Ken, when I run your demo file, I got this error message:

Parser Error Message: Could not load type 'p4320work.csv' .
Source Error:
Line 1: <%@ Page Language="vb" AutoEventWireup ="false"
Codebehind="Web Form1.aspx" Inherits="p4320 work.csv"%>
Line 2: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Line 3: <HTML>

Even I made a fake 'p4320work.csv' which is 0 byte, still not works. Did this
means I need a real 'p4320work.csv' in the project folder?

Thanks.
J.

On Mon, 27 Sep 2004 20:36:58 -0400, "Ken Cox [Microsoft MVP]"
<BA************ @sympatico.ca> wrote:
Hi J,

Here's some code that should get you going. It grabs data from the SQL
database as a datareader and pushes it out as a CSV. No storage of the file
required. You should see Excel open with the data.

Does this help?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="csv .aspx.vb"
Inherits="p432 0work.csv"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>csv</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="htt p://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "FlowLayout ">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Launch"></asp:Button>
</form>
</body>
</HTML>


Nov 18 '05 #4
That was sample code that won't run on a different system without changes.

The part you mention is a reference to the class in the codebehind.
p4320work is the name of my project and csv was the name of my class.

You need to change it to the name of the class in your own page. It will be
something like myprojectname.w ebform1 .

You'll also need to change the SQL Server config and password info too.

"J" wrote:
Hi, Ken, when I run your demo file, I got this error message:

Parser Error Message: Could not load type 'p4320work.csv' .
Source Error:
Line 1: <%@ Page Language="vb" AutoEventWireup ="false"
Codebehind="Web Form1.aspx" Inherits="p4320 work.csv"%>
Line 2: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Line 3: <HTML>

Even I made a fake 'p4320work.csv' which is 0 byte, still not works. Did this
means I need a real 'p4320work.csv' in the project folder?

Thanks.
J.

On Mon, 27 Sep 2004 20:36:58 -0400, "Ken Cox [Microsoft MVP]"
<BA************ @sympatico.ca> wrote:
Hi J,

Here's some code that should get you going. It grabs data from the SQL
database as a datareader and pushes it out as a CSV. No storage of the file
required. You should see Excel open with the data.

Does this help?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="csv .aspx.vb"
Inherits="p432 0work.csv"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>csv</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="htt p://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "FlowLayout ">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Launch"></asp:Button>
</form>
</body>
</HTML>


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6783
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to achieve this is to write the report to a temporary file, or more likely to a temporary directory with a separate file for each page. I can use that for previewing and for printing, and then delete the file/directory. There seems to be a few ways to...
7
8859
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
4
4414
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create shortcut to my program/utility (2) Entry in Windows' Start --> Program Menu. Actually in my VB.Net solution I have two projects (1) MYPROGRAM (2) MYPROGRAM_INSTALLER. MYPROGRAM is a "Windows Application". MYPROGRAM_INSTALLER is a "SetUp Wizard"...
5
6770
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
8
20362
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1: Start with original thumbnails & two empty sub directories STEP 2: Create smaller versions of the originals for one sub directory STEP 3: Create thumbnail version of the originals the other sub directory STEP 4: Create an index.html pointing to the...
23
7402
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
4
6907
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
3
3948
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
3
2049
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt"; if(!chmod($path, 0744)) { echo "error, $path"; exit; } else {
15
5269
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.