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.