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; |
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!
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 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) End Sub 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 Next iFor j = 0 To ds.Tables(0).Columns.Count - 1 str.Append(ds.Tables(0).Rows(i)(j).ToString()) Next j str.Append("<BR>") 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() |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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> |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
The flow for the code behind page is as follows.
| 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 ' End Class 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 End SubDim cn As New SqlConnection(ConnectionString) End Ifcn.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() Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim cn As New SqlConnection(ConnectionString) End Subcn.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 Next iFor j = 0 To ds.Tables(0).Columns.Count - 1 str.Append(ds.Tables(0).Rows(i)(j).ToString()) Next j str.Append("<BR>") 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() Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub |
No comments:
Post a Comment