Rahul Sharma

Rahul Sharma

  • NA
  • 194
  • 13.9k

Error while downloading pdf stored in database

Sep 18 2019 7:04 AM
Getting this error:  Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
  1. int id = int.Parse((sender as LinkButton).CommandArgument);  
  2.        byte[] bytes;  
  3.        string fileName, contentType;  
  4.        string constr = ConfigurationManager.ConnectionStrings["JKPSC"].ConnectionString;  
  5.        try  
  6.        {  
  7.            using (SqlConnection con = new SqlConnection(constr))  
  8.            {  
  9.                using (SqlCommand cmd = new SqlCommand())  
  10.                {  
  11.                    cmd.CommandText = "select id, pdfType, pdfdata, regNo from tbl_Doc where id=@Id";  
  12.                    cmd.Parameters.AddWithValue("@Id", id);  
  13.                    cmd.Connection = con;  
  14.                    con.Open();  
  15.                    using (SqlDataReader sdr = cmd.ExecuteReader())  
  16.                    {  
  17.                        sdr.Read();  
  18.                        bytes = (byte[])sdr["pdfdata"];  
  19.                         
  20.                        fileName = sdr["pdfType"].ToString() + sdr["regNo"].ToString() + ".pdf";  
  21.                    }  
  22.                    con.Close();  
  23.                }  
  24.            }  
  25.            Response.Clear();  
  26.            Response.Buffer = true;  
  27.            Response.Charset = "";  
  28.            Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  29.            Response.ContentType = "application/pdf";  
  30.            Response.AppendHeader("Content-Disposition""attachment; filename=" + fileName);  
  31.            Response.BinaryWrite(bytes);  
  32.            Response.Flush();  
  33.            Response.Close();  
  34.            Response.End();  
  35.   
  36.   
  37.        }  
  38.        catch (Exception ex)  
  39.        {  
  40.            throw;  
  41.        }  
 How to solve this error

Answers (3)