Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually i have written code but that record is downloading in to my system
so below is my code any body help me where is the error

C#
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.Data.SqlClient;
using System.Net;

public partial class dropdown_to_gridview : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }
    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)
    {
        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();
        
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=report.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        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, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        pdfFrame.Attributes.Add("sr", "pdfDoc");
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
   
}
Posted
Updated 12-Aug-15 3:42am
v2

1 solution

To put the file in the iframe you'll need to see the iframe's src property on the client. When someone changes the drop down update the src of the iframe via javascript to something like "/showpdf.aspx?id=???" where ??? is the value of the selected item. Now create a page called showpdf.aspx and in its page_load event put the code that is currently in your _click event only rather than getting a value from the drop down, get it from the querystring

C#
SqlCommand cmd = new SqlCommand("select * from employeep where Salary ='" + Request.QueryString["id"] + "'", con);


In terms of your query you shouldn't use "select *", always name your columns, and don't make SQL that way as it is open to sql injection attacks, used parameterised queries instead.

To answer your next question, not I won't write all the code for you. If you don't know how to run javascript when the dropdown changes then google "javascript select change event". To read the selected value google "javascript get select value on change". To update the iframe's src google "javascript update iframe src". Then you just have to put everything together.
 
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