asked on
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="cmdSend" runat="server" Text="Download File"
onclick="cmdSend_Click" /></br>
</br>
<asp:GridView ID="GridView1" AutoGenerateColumns="True" runat="server" />
</div>
</form>
</body>
</html>
aspx.cs backendusing System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private const string MDBFILE = "~/hgdeposition/App_Data/hgweb.mdb";
private string GetConnectionString()
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";
}
protected void Page_Load(object sender, EventArgs e)
{
string SQL = "SELECT * FROM RainEvents";
if(!Page.IsPostBack){
// Create Connection object
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());
// Create Command Object
OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);
// Open Connection
dbConn.Open();
// Execute command and receive DataReader
OleDbDataReader dbRead = dbComm.ExecuteReader();
GridView1.DataSource = dbRead;
GridView1.DataBind();
dbConn.Close();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
/// <summary>
/// This event is used to export gridview data to CSV document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void cmdSend_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "File.csv"));
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
StringBuilder strbldr = new StringBuilder();
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
//separting header columns text with comma operator
strbldr.Append(cell.Text + ',');
}
//appending new line for gridview header row
strbldr.Append("\n");
foreach (GridViewRow gvr in GridView1.Rows)
{
foreach (TableCell cell in gvr.Cells)
{
//separating gridview columns with comma
strbldr.Append(cell.Text + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Write(strbldr.ToString());
Response.End();
}
}
ASKER
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="cmdSend" runat="server" Text="Download File"
onclick="cmdSend_Click" /></br>
</br>
<asp:GridView ID="GridView1" AutoGenerateColumns="True" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private const string MDBFILE = "~/hgdeposition/App_Data/hgweb.mdb";
private string GetConnectionString()
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";
}
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// This event is used to export datatable to CSV document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void cmdSend_Click(object sender, EventArgs e)
{
string SQL = "SELECT * FROM RainEvents";
DataTable dTable = new DataTable();
OleDbDataAdapter dbadp = new OleDbDataAdapter(SQL, GetConnectionString());
dbadp.Fill(dTable);
dbadp.Dispose();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "File.csv"));
Response.ContentType = "application/text";
StringBuilder strbldr = new StringBuilder();
foreach (DataColumn col in dTable.Columns)
{
//separting header columns text with comma operator
strbldr.Append(cell.Text + ',');
}
//appending new line for gridview header row
strbldr.Append("\n");
foreach (DataRow row in dTable.Rows)
{
foreach (Object cell in row.ItemArray)
{
//separating gridview columns with comma
strbldr.Append((string)cell + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Write(strbldr.ToString());
Response.End();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="cmdSend" runat="server" Text="Download File"
onclick="cmdSend_Click" /></br>
</br>
</div>
</form>
</body>
</html>
ASKER
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private const string MDBFILE = "~/hgdeposition/App_Data/hgweb.mdb";
private string GetConnectionString()
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";
}
protected void Page_Load(object sender, EventArgs e)
{
string SQL = "SELECT * FROM RainEvents";
if(!Page.IsPostBack){
// Create Connection object
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());
// Create Command Object
OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);
// Open Connection
dbConn.Open();
// Execute command and receive DataReader
OleDbDataReader dbRead = dbComm.ExecuteReader();
GridView1.DataSource = dbRead;
GridView1.DataBind();
dbConn.Close();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
/// <summary>
/// This event is used to export gridview data to CSV document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void cmdSend_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "File.csv"));
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
StringBuilder strbldr = new StringBuilder();
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
//separting header columns text with comma operator
strbldr.Append(cell.Text + ',');
}
//appending new line for gridview header row
strbldr.Append("\n");
foreach (GridViewRow gvr in GridView1.Rows)
{
foreach (TableCell cell in gvr.Cells)
{
//separating gridview columns with comma
strbldr.Append(cell.Text.Replace(" ", String.Empty) + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Write(strbldr.ToString());
Response.End();
}
}
your are saying that I should remove my protected void cmdSend_Click portion of code?Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "File.csv"));
Response.ContentType = "application/text";
string SQL = "SELECT * FROM FSUdata ";
// Create Connection object
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());
// Create Command Object
OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);
// Open Connection
dbConn.Open();
StringBuilder strbldr = new StringBuilder();
// Execute command and receive DataReader
using (OleDbDataReader dbRead = dbComm.ExecuteReader())
{
for (int i=0; i< dbRead.FieldCount;i++)
{
//separting header columns text with comma operator
strbldr.Append(dbRead.GetName(i) + ',');
}
//appending new line for gridview header row
strbldr.Append("\n");
while (dbRead.Read())
{
for (int i=0; i< dbRead.FieldCount;i++)
{
//separating gridview columns with comma
strbldr.Append(Convert.ToString(dbRead[i]) + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
}
Response.Write(strbldr.ToString());
Response.End();
ASKER
ASKER
ASKER
protected void Page_Load(object sender, EventArgs e)
{
}
ASKER
The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications
TRUSTED BY
Please do refer below sample code.
Open in new window