This tutorial will show you how to export GridView to txt using ASP.NET 2.0 and C#.
First, you need to import the namespace from System.Data.SqlClient.
| using System.Data.SqlClient; |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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 void Button1_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(ConnectionString); cn.Open(); cn1 = new SqlConnection(ConnectionString); cn1.Open(); DataSet ds = new DataSet(); SqlDataAdapter ad = new SqlDataAdapter("select * from [authors]", cn); ad.Fill(ds); StringBuilder str = new StringBuilder(); for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) }{ str.Append(ds.Tables[0].Rows[i][j].ToString()); }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"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); Response.Write(str.ToString()); Response.End(); cn.Close(); cn1.Close(); cn.Dispose(); cn1.Dispose(); } |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
The front end GridViewExportTxtCsharp.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> |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
The flow for the code behind page is as follows.
| using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Text; public partial class _Default : System.Web.UI.Page { string ConnectionString = "Data Source=(local);Initial Catalog=pubs;User Id=sa;Password=sa123"; } SqlConnection cn1; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) }{ SqlConnection cn = new SqlConnection(ConnectionString); cn.Open(); cn1 = new SqlConnection(ConnectionString); cn1.Open(); SqlCommand cmd = new SqlCommand("select * from [authors]", cn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = dr; GridView1.DataBind(); dr.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(ConnectionString); cn.Open(); cn1 = new SqlConnection(ConnectionString); cn1.Open(); DataSet ds = new DataSet(); SqlDataAdapter ad = new SqlDataAdapter("select * from [authors]", cn); ad.Fill(ds); StringBuilder str = new StringBuilder(); for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++) }{ str.Append(ds.Tables[0].Rows[i][j].ToString()); }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"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); Response.Write(str.ToString()); Response.End(); cn.Close(); cn1.Close(); cn.Dispose(); cn1.Dispose(); } public override void VerifyRenderingInServerForm(Control control) { } |
No comments:
Post a Comment