Hi,
I have created a grid view and give option to download the flle from different table by passing ID as asp linked button in command argument.
my code is working and downloading the flle as well but it works only first time or any record only once after that If I click on any other downalod button in grid in any other row it wont do anything.
This is my asp linked button code in grid.
<asp:LinkButton ID="LinkButton1" runat="server" Text="<img src='/file_pdf.png' alt='Download' border='0'/>"
OnClick="DownloadFile_Test" CommandArgument='<%# Eval("Sno") %>'></asp:LinkButton>
Download button in grid On CLick Event code
protected void DownloadFile_Test(object sender, EventArgs e)
{
int File_id = int.Parse((sender as LinkButton).CommandArgument);
// int File_id = Convert.ToInt32((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName;
using (SqlConnection con = new SqlConnection(getconn()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select FileName, ContentData from Test where TestID=@SNId";
cmd.Parameters.AddWithValue("@SNId", File_id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["ContentData"];
//contentType = sdr["ContentType"].ToString();
fileName = sdr["FileName"].ToString();
}
con.Close();
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
// HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
If any one can help me that this code should work for all the records in the grid not only one time.
Thanks