Click here to Skip to main content
15,896,457 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have written code i have make the pdf doc to be download to a specific folder and that folder path i have given as source to iframe but when button is clicked pdfdoc is downloading to a specific folder but not showing in iframe
below is the code so any body helpme where my problem is


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Net;

public partial class dropdown_to_gridview : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind();
}
public void GridViewBind()
{
string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + DropDownList1.SelectedValue + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void btnExportPDF_Click(object sender, EventArgs e)
{

// Response.ContentType = "application/pdf";
// Response.AddHeader("content-disposition",
// "attachment;filename=report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);

FileStream fs = new FileStream(@"D:\Programs\raj.pdf", System.IO.FileMode.Create);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();

iframepdf.Attributes["src"] = @"D:\Programs\raj.pdf";
}


public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Posted

1 solution

Firstly, your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
SQL injection attack mechanics | Pluralsight [^]




Secondly, you are saving the PDF file on the server. You are then passing the local path of the file on the server back to the client and asking it to display that file.

That will not work. Either the client will try to open the file in it's own D: drive, which will fail as the file doesn't exist; or, more likely, the browser will refuse to attempt to open a local file in an iframe from an internet site.

You need to point the iframe to a URL on your server which will generate the PDF and send it back to the client. Something like this should work:
C#
public partial class dropdown_to_gridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"] == "export")
            {
                string salary = Request.QueryString["salary"];
                GridViewBind(salary);
                ExportPdf();
                Response.End();
            }
        }
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewBind(DropDownList1.SelectedValue);
    }
    
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {
        iframepdf.Attributes["src"] = Request.Path + "?action=export&salary=" + HttpUtility.UrlEncode(DropDownList1.SelectedValue);
    }
    
    private void GridViewBind(string salary)
    {
        const string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
        
        using (SqlConnection con = new SqlConnection(cs))
        using (SqlCommand cmd = new SqlCommand("select * from employeep where Salary = @Salary", con))
        {
            cmd.Parameters.AddWithValue("@Salary", salary);
            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    
    private void ExportPdf()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=report.pdf");
        
        StringWriter sw = new StringWriter();
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView1.RenderControl(hw);
        }
        
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        
        pdfDoc.Open();
        htmlparser.Parse(new StringReader(sw.ToString()));
        pdfDoc.Close();
    }
    
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
}
 
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