数据库中检索arecord并在选择下拉框时在iframe中显示为

数据库中检索arecord并在选择下拉框时在iframe中显示为

本文介绍了如何从数据库中检索arecord并在选择下拉框时在iframe中显示为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我已编写代码,但该记录正在下载到我的系统

所以下面是我的代码任何正文帮助我哪里是错误



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

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 */
    }

}

推荐答案

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





就你的查询而言,你不应该使用select *,总是命名你的列,并且不要那样做SQL,因为它是开放的sql注入攻击,改为使用参数化查询。



要回答你的下一个问题,我不会为你编写所有代码。如果你不知道如何在下拉列表更改时运行javascript,那么googlejavascript select change event。要阅读所选值谷歌javascript获取更改选择值。要更新iframe的src googlejavascript update iframe src。然后你只需把所有东西放在一起。



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.


这篇关于如何从数据库中检索arecord并在选择下拉框时在iframe中显示为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:29