Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When im Exporting img,Textboxvalues,Gridview to PDF generating statuscode 500 Error the Following is the code that i've written to generate the pdf. It's working fine locally or at server side also.. but When it hosted in live.. the problem araised.. Generating statuscode 500 Error during Onclick of PDFConversion Button.. How can i resolve such a live issue..

C#
#region 
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        //To Export all pages
                        grdaccinfodatewise.AllowPaging = false;
                        //Calling the Function BindGrid(), here im adding the data to the Datasource and diplaying in Grid
                        BindGrid();
                        
                        grdaccinfodatewise.RenderControl(hw);
                        StringReader sr = new StringReader(sw.ToString());
                        Document pdfDoc = new Document(PageSize.A3, 10f, 10f, 10f, 10f);
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                        pdfDoc.Open();
                        pdfDoc.NewPage();
                        string imageURL = Server.MapPath(".") + "/img/bank_logo.png";
                        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);                       
                        jpg.SpacingBefore = 10f;                       
                        jpg.SpacingAfter = 1f;
                        jpg.Alignment = Element.ALIGN_CENTER;
                        pdfDoc.Add(jpg);
                        if (rdbtnbydate.Checked == true)
                        {

                            pdfDoc.Add(new Phrase("\r\nAccount Statement From " + this.dateMinFilter.Text.Trim() + " To " + this.dateMaxFilter.Text.Trim() + "\r\n\r\n"));
                            pdfDoc.Add(new Phrase("ACCOUNT NUMBER : " + Session["loginaccno"].ToString().Trim()));
                            pdfDoc.Add(new Phrase("\r\nCUSTOMER NAME : " + Session["CustName"].ToString().ToUpper().Trim()));
                            pdfDoc.Add(new Phrase("\r\nAccount Type : " + ViewState["SchemeName"].ToString().ToUpper().Trim()));                            
                        }
                        if (rdbtnmonth.Checked == true)
                        {

                            pdfDoc.Add(new Phrase("\r\nAccount Statement for the Month of : " + this.ddlmonth.SelectedItem.ToString().ToUpper().Trim() + " - " + this.txtyear.Text.Trim() + "\r\n"));
                            pdfDoc.Add(new Phrase("\r\nCUSTOMER NAME : " + Session["CustName"].ToString().ToUpper().Trim()));
                            pdfDoc.Add(new Phrase("\r\nACCOUNT NUMBER : " + Session["loginaccno"].ToString().Trim() + "\r\n\r\n"));
                        }

                        htmlparser.Parse(sr);
                        pdfDoc.Close();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "attachment;filename=pdfExport.pdf");
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.Write(pdfDoc);                        
                        HttpContext.Current.ApplicationInstance.CompleteRequest();
                    }
                }         
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
#endregion
Posted
Updated 18-Apr-16 3:19am
v12
Comments
F-ES Sitecore 11-Apr-16 4:39am    
It's more likely your code isn't running. 404 basically means it can't find the page you are trying to show so you need to look at the url that you're trying to access and work out why it can't be found.
JanardhanSharma 11-Apr-16 6:13am    
Im sorry.. I found the status code of the error.. it's not 404 rather it's 500 saying internal server error. I used custom error pages..
I would suggest you to handle the exception and log it in a text file or something instead of throwing it. This way, you can find out the problem if you log the details.
Richard Deeming 18-Apr-16 11:26am    
Never use throw ex; to rethrow an exception - it destroys the stack trace, and makes it impossible to track down the real source of the error.

If you absolutely must rethrow an exception, use throw; instead:
catch (Exception ex)
{
   // Do something with the exception here...
   throw;
}


However, in this case, since you're not doing anything with the exception, just remove the try..catch block and replace it with the contents of the try part.

1 solution

its i think issue .Net Framework or Folder Permission Issue.

1. IIS in Application pool are check >Net Framework version are config and pool are same.

2. other wise you render PDF file this folder are everyone user and IIS_user persion this folde

then try
 
Share this answer
 
Comments
JanardhanSharma 12-Apr-16 3:14am    
the following procedure resolved the issue
http://www.aspsnippets.com/Articles/ASPNet-iTextSharp-SystemSecuritySecurityException-That-assembly-does-not-allow-partially-trusted-callers.aspx
Arvind Zamakia 12-Apr-16 8:10am    
right click on dll and goto property -> security tab -> Advance -> add everyone user permision
JanardhanSharma 13-Apr-16 0:48am    
can we do lyk this also? anyhow thanq.. it's working now..
and may i knw whether using


<trust level="Full" />

under system.web in web.config

is this a good practice?

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