

Navigator : Home > Tutorials > File Tutorials > ...
How to export GridView to txt file using VB.NET
This tutorial will show you how to export GridView to txt using ASP.NET 2.0 and VB.
First, you need to import the namespace from System.Data.SqlClient.
| Imports System.Data.SqlClient; |
The System.Data.SqlClient namespace contains The System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server.The .NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space. In tutorial, we need "AddHeader" and "ContentType" to do the work.The AddHeader method adds a new HTML header and value to the response sent to the client. It does not replace an existing header of the same name. After a header has been added, it cannot be removed. The ContentType property specifies the HTTP content type for the response. If no ContentType is specified, the default is text/HTML.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
We use the Button1_Click event to do the work. We then call "Response.AddHeader" to export a file which is named FileName.txt. We then use "Response.ContentType" to denotes the type of the file being exported.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cn As New SqlConnection(ConnectionString) cn.Open() cn1 = New SqlConnection(ConnectionString) cn1.Open() Dim ds As New DataSet() Dim ad As New SqlDataAdapter("select * from [authors]", cn) ad.Fill(ds) Dim str As New StringBuilder() Dim i As Integer For i = 0 To ds.Tables(0).Rows.Count - 1
Dim j As Integer For j = 0 To ds.Tables(0).Columns.Count - 1 str.Append(ds.Tables(0).Rows(i)(j).ToString()) Next j
str.Append("<BR>") Next i Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=FileName.txt") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.text" Dim stringWrite As New System.IO.StringWriter() Dim htmlWrite = New HtmlTextWriter(stringWrite) Response.Write(str.ToString()) Response.End() cn.Close() cn1.Close() cn.Dispose() cn1.Dispose() End Sub |
The front end GridViewExportTxtVB.aspx page looks something like this:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Export to Word" width="99px" /><br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> |
The flow for the code behind page is as follows.
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports System.Data.SqlClient Imports System.Text
Class _Default
Inherits System.Web.UI.Page ' Private ConnectionString As String = "Data Source=(local);Initial Catalog=pubs;User Id=sa;Password=sa123" Private cn1 As SqlConnection Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim cn As New SqlConnection(ConnectionString) cn.Open() cn1 = New SqlConnection(ConnectionString) cn1.Open() Dim cmd As New SqlCommand("select * from [authors]", cn) Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) GridView1.DataSource = dr GridView1.DataBind() dr.Close() End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cn As New SqlConnection(ConnectionString) cn.Open() cn1 = New SqlConnection(ConnectionString) cn1.Open() Dim ds As New DataSet() Dim ad As New SqlDataAdapter("select * from [authors]", cn) ad.Fill(ds) Dim str As New StringBuilder() Dim i As Integer For i = 0 To ds.Tables(0).Rows.Count - 1
Dim j As Integer For j = 0 To ds.Tables(0).Columns.Count - 1 str.Append(ds.Tables(0).Rows(i)(j).ToString()) Next j
str.Append("<BR>") Next i Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=FileName.txt") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.text" Dim stringWrite As New System.IO.StringWriter() Dim htmlWrite = New HtmlTextWriter(stringWrite) Response.Write(str.ToString()) Response.End() cn.Close() cn1.Close() cn.Dispose() cn1.Dispose() End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub End Class |
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!