http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
i use this artical for exporting gridview to Excel in asp.net - c#.....but i found some error in my code
i am using stored procedure for sql command and my code behind is as follows....
C# code
public partial class Admin_ResultDisplay : System.Web.UI.Page
{
SqlConnection cn;
protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
//string strQuery = "select CustomerID,City,Country,PostalCode from customers";
//SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd); gives error here <= cmd is not exist in current context
GridView1.DataSource = dt;
GridView1.DataBind();
lblStatus.Text = "";
lblHeader.Text = "Select Branch To Search Reasult";
}
protected void btn_insert_Click(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("GetExamResults", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("Course_Id", ddlCourseId.SelectedValue);
cmd.Parameters.AddWithValue("Semester", ddlSemester.SelectedValue);
cmd.Parameters.AddWithValue("Exam_Type", ddlExamType.SelectedValue);
cmd.Connection.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
GridView1.Visible = true;
lblHeader.Text = ddlCourseId.SelectedValue + " [ Semester - " + ddlSemester.SelectedValue + " ]";
cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
// lblStatus.Text = ex.Message;
GridView1.Visible = false;
lblStatus.Text = "No Records Found. !!";
lblHeader.Text = "Please Try Another Alternative...";
}
}
private DataTable GetData(SqlCommand cmd) <= how can i pass cmd here ??gives error while posr back page
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString;
SqlConnection cn = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
try
{
cn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
sda.Dispose();
cn.Dispose();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void lnkbtnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
//GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
//GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
//GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
//GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
GridViewRow row = GridView1.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
//if (i % 2 != 0)
//{
// row.Cells[0].Style.Add("background-color", "#C2D69B");
// row.Cells[1].Style.Add("background-color", "#C2D69B");
// row.Cells[2].Style.Add("background-color", "#C2D69B");
// row.Cells[3].Style.Add("background-color", "#C2D69B");
//}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
how can i pass sql command at page_load event ? ?