

Navigator : Home > Tutorials > File Tutorials > ...
How to export GridView to txt file using C#
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; |
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.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
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(); } |
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> |
The flow for the code behind page is as follows.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
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) { } } |
Looking for the VB.NET2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!