Wednesday, October 04, 2006

Creating Files using ASP.NET code behind VB.NET -- Save a file to a specific directory

In ASP.NET we can create a file and save it in a particular directory, to do the operation we have two different ways. One is using Response.Write("STRING") and another one is filestream.

First let me give you the sample code using Response.Write("STRING").

Code Sample using Response.Write("STRING"):

Imports System
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm1
Inherits System.Web.UI.Page

Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim filename As String
Dim dr As SqlDataReader
Dim i As Integer
Dim sb As System.Text.StringBuilder

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
cn = New SqlConnection("server=localhost;uid=sa;pwd=;database=northwind")
filename = "products.csv"
cmd = New SqlCommand("select * from products ", cn)
cmd.Connection.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
sb = New System.Text.StringBuilder


'For field Names
For i = 0 To dr.FieldCount - 1
If i < (dr.FieldCount - 1) Then

sb.Append(Chr(34) & dr.GetName(i) & _ Chr(34) & ",")
Else
sb.Append(Chr(34) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;
dr.GetName(i) & _ Chr(34) & vbCrLf)
End If Next

'For field Values
While dr.Read()
For i = 0 To dr.FieldCount - 1
If i < (dr.FieldCount - 1)
Then
sb.Append(Chr(34) & _ dr.GetValue(i).ToString & Chr(34) & ",")
Else
sb.Append(Chr(34) & _ dr.GetValue(i).ToString & Chr(34) & vbCrLf)
End If Next
End
While dr.Close()
cn.Close()
Response.ContentType = "Application/x-msexcel" Response.AddHeader _ ("content-disposition", "attachment; filename=""" & _ filename & """")

'Write the file directly to the HTTP output stream.

Response.Write(sb.ToString)
Response.End()
End Sub
End Class

Code Sample using filestream:

Imports System
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO

Public Class WebForm1
Inherits System.Web.UI.Page

Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim filename As String
Dim dr As SqlDataReader
Dim i As Integer
Dim sb As System.Text.StringBuilder


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cn = New SqlConnection("server=localhost;uid=sa;pwd=;database=northwind")
filename = "products.csv"
cmd = New SqlCommand("select * from products ", cn)
cmd.Connection.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
sb = New System.Text.StringBuilder

'For field Names
For i = 0 To dr.FieldCount - 1
If i < (dr.FieldCount - 1)

Then
sb.Append(Chr(34) & dr.GetName(i) & Chr(34) & ",")
Else sb.Append(Chr(34) &amp;amp;amp;amp;amp;amp;amp;
dr.GetName(i) & Chr(34) & vbCrLf)
End If
Next
'For field Values
While dr.Read()
For i = 0 To dr.FieldCount - 1
If i < (dr.FieldCount - 1)
Then
sb.Append(Chr(34) & dr.GetValue(i).ToString & Chr(34) & ",")
Else
sb.Append(Chr(34) & dr.GetValue(i).ToString & Chr(34) & vbCrLf)
End If
Next
End While
dr.Close()
cn.Close()
// FileStream to create files
Dim fs As New FileStream("c:\products0.csv", FileMode.Create, FileAccess.Write)
Dim s As New StreamWriter(fs)
s.BaseStream.Seek(0, SeekOrigin.End)
s.WriteLine(sb.ToString) s.Close()
End Sub
End Class


These are the two simple ways to create files using VB.NET.

2 comments:

Anonymous said...

This is really cool.I found exactly what i was lloking for. You made my day.

Thanks,
AK

Anonymous said...

Thanks....!! Its real helpful.

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...