How to export data from SQL Server database to Text file using ASP.Net with C# 2.0


In this Article, I am going to Explain about how to export data from SQL Server database to Text file using ASP.NET with C# 2.0. I have Seen too many members asking how to export data from SQL Server database to Text file using ASP.NET with C# 2.0. So I am deciding to write article about how to export data from SQL Server database to Text file using ASP.NET with C# 2.0.

Export Data from SQL Server database to Text file using ASP.Net with C# 2.0


Today We are going to Learn about How to Export Data from SQL Server database to Text file using ASP.Net with C# 2.0. I have Seen too many members asking How to Export Data from SQL Server database to Text file using ASP.Net with C# 2.0. So I am deciding to write article about How toExport Data from SQL Server database to Text file using ASP.Net with C# 2.0.

Some times we need to Export Data from SQL Server database to Text file or Excel File.

We need to use following Namespace in this application.

using System.Data.SqlClient;
using System.Data;
using System.Configuration;

Context.Response.AppendHeader is a method which is used to Adds an HTTP header to the output stream.

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".txt");
context.Response.End();

Write Below code in Web.config file Configuration section

<add name="SurveyConnectionString" connectionString="data source=Suresh;database=MyLearning;uid=sa;pwd=123456;"
providerName="System.Data.SqlClient" />

To get Connection string from web.config file.

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYLearningConnectionString"].ConnectionString);

Write Below code in ExportText.aspx.cs.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class ExportText : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MYLearningConnectionString"].ConnectionString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tblUsers", conn);
DataSet ds = new DataSet();
da.Fill(ds);

ExportToText(ds.Tables[0], "DataTable");


}

protected void ExportToText(DataTable dataTable, string fileName)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dataTable.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);

foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
context.Response.Write(row[i].ToString() + ",");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".txt");
context.Response.End();
}
}

if you want to Export data into CSV file from SQL Server Database. Please Change following code.

context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
context.Response.End();

I think It will help you about how to export data from SQL Server database to Text file using ASP.NET with C# 2.0 . Thanks for reading my Article how to export data from SQL Server database to Text file using ASP.NET with C# 2.0. if you have any query or you have any suggestion, let me know. I will appreciate you valuable feedback.

if you want to know about How to Create Login Page in ASP.Net Applications using Session. Please Refer Following Link.
How to Create Login Page in ASP.Net Applications using Session.
if you want to know about How to Upload Excel file in ASP.Net Applications. Please Refer Following Link.
Excel file upload in asp.net 2.0 display in grid view
if you want to know about How to create Alphanumeric Auto Increment in ASP.NET from SQL Server. Please Refer Following Link.
How to create Alphanumeric Auto Increment in ASP.NET from SQL Server.

Thanks

S.Suresh


Comments

Guest Author: Nilesh Chaudhari23 Apr 2012

Hi,
Really nice articale.I use this code..I need in ASP.Net 3.5 also anything new added Please guide..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: