Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

Download word file from gridview control using asp.net?
here small error is coming in below please check and reply me please

source code:
C#
protected void Page_Load(object sender, EventArgs e)
{
grid();
  }

private void grid()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
        string query = "select Name,Photo from ImageUpdate";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string filename = "select Name,Photo from ImageUpdate where Email=@Email";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
string aaa = Server.MapPath("~/ProductImage/" + filename);
Response.TransmitFile(Server.MapPath("~/ProductImage/" + filename));
Response.End();
}
Posted
Updated 7-Aug-14 20:05pm
v2
Comments
Mohamed Rafiq K 8-Aug-14 2:04am    
what type of exception your getting
member1431 8-Aug-14 2:09am    
Generally this is my code here download properly but no data coming like empty
please check and reply now please

Source code:

public partial class Images : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
SqlDataAdapter sda;
SqlCommand cmd;
DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
cmd = new SqlCommand("select * from ImageUpdate where Email=@Email", con);
cmd.Parameters.AddWithValue("@Email", Session["Email"].ToString());
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds, "ImageUpdate");

lblMail.Text = ds.Tables["ImageUpdate"].Rows[0][2].ToString();
imgupd.ImageUrl = ds.Tables["ImageUpdate"].Rows[0][3].ToString();

if (!IsPostBack)
{
grid();
}
}
private void grid()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
string query = "select Name,Photo from ImageUpdate";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("select Name,Photo from ImageUpdate where Email=@Email", con);
cmd.Parameters.AddWithValue("@Email", Session["Email"].ToString());
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = sdr["Photo"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + sdr["Name"].ToString());
//Response.Charset = "";
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.BinaryWrite((byte[])sdr["Mobile"]);
Response.End();
}
}
}

Kumarbs 8-Aug-14 8:11am    
Where did you write the data to the excel file?
member1431 8-Aug-14 8:40am    
is the default excel file actually retrieve and store in folder of solution explorer

that folder to retrieve and download here default excel or image anything please reply me
Kumarbs 11-Aug-14 0:09am    
Make sure the content type is "application/vnd.ms-excel". You Can refer
http://www.c-sharpcorner.com/uploadfile/0c1bb2/uploading-and-downloading-excel-files-from-database-using-as/

1 solution

Below link gives upload and download file to gridview you can refer only download part

Upload and download file[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900