Hi all,
I have generated one application using asp.net with c#.
The concept is,
when i click button, the data has been display excel sheet, but in my application its getting few errors like "incorrect syntax near datadownload". using stored procedure am retrieving the data from database, please give me a solution for this app..
my code is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.IO;
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 _Default : System.Web.UI.Page
{
string s = ConfigurationManager.AppSettings["const"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
private DataTable Getdata(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(s);
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
da.Dispose();
con.Dispose();
}
}
protected void download_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(s);
SqlCommand cmd = new SqlCommand("datadownload", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Destinations", SqlDbType.VarChar);
if (con.State == ConnectionState.Closed)
{
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
DataTable dt = Getdata(cmd);
GridView gv1 = new GridView();
gv1.AllowPaging = false;
gv1.DataSource = dt;
gv1.DataBind();
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);
for (int i = 0; i < gv1.Rows.Count; i++)
{
gv1.Rows[i].Attributes.Add("class", "textmode");
}
gv1.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}