locked
pdf is not working with ajax method RRS feed

  • Question

  • User1839056048 posted

    following is a working code for displaying pdf report in browser.

     protected void btnpdf_Click(object sender, EventArgs e)
            {
                string strQuery = "select * from dscn";
                SqlCommand cmd = new SqlCommand(strQuery);
    
                DataTable dt = GetData(cmd);
                GridView GridView1 = new GridView();
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Response.ContentType = "application/pdf";
                Response.AppendHeader("Content-Disposition", string.Format("inline; filename={0}", "Customers.pdf"));
                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();
            }

      

     private DataTable GetData(SqlCommand cmd)
            {
    
                DataTable dt = new DataTable();
                SqlDataAdapter sda = new SqlDataAdapter();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                try
                {
    
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                    return dt;
    
                }
    
                catch (Exception ex)
                { throw ex;}
    
                finally
                {
    
                    con.Close();
                    sda.Dispose();
                    con.Dispose();
    
                }
    
            }

    it was in the click event of button

    my requirement is to use above example using jquery like this

                $('#btnpdf').click(function (e) {
                    e.preventDefault();
                    var query = "select * from dscn";
                    var format = $('#DRpformat').val();
                    var sclass = $('#txtclass').val();
                    var Data = 'id=' + query + '&format=' + format;
                    $.ajax({
                        type: "GET",
                        data: Data,
                        contentType: "text/html; charset=utf-8",
                        url: "PdfProcess.aspx",
                        success: function (data) {
                           // alert(data.d);
    
                        }
                    });
                });

    pdfprocess

     string id;
            string format;
            protected void Page_Load(object sender, EventArgs e)
            {
    
               id = Request.QueryString["id"].ToString();
              format = Request.QueryString["format"].ToString().Trim();
              if (format == "PDF")
              {
                  Pdf(id);
              }
    }
    
     public void Pdf(string id)
            {
               
              
                string strQuery = "" + id + ""; 
                SqlCommand cmd = new SqlCommand(strQuery);
    
                DataTable dt = GetData(cmd);
                GridView GridView1 = new GridView();
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Response.ContentType = "application/pdf";           
                Response.AppendHeader("Content-Disposition", string.Format("inline; filename={0}","Customers.pdf"));            
                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();
               
            }
    
    private DataTable GetData(SqlCommand cmd)
            {
    
                DataTable dt = new DataTable();
    
    
    
                SqlDataAdapter sda = new SqlDataAdapter();
    
                cmd.CommandType = CommandType.Text;
    
                cmd.Connection = con;
    
                try
                {
    
                    con.Open();
    
                    sda.SelectCommand = cmd;
    
                    sda.Fill(dt);
    
                    return dt;
    
                }
    
                catch (Exception ex)
                {
    
                    throw ex;
    
                }
    
                finally
                {
    
                    con.Close();
                    sda.Dispose();
                    con.Dispose();
    
                }
    
            }

    but is not working how to solve this

    Regards

    Baiju

    Thursday, February 11, 2016 1:36 PM

Answers

  • User475983607 posted

    how to use that can you some example

    Like this...

    $('#btnpdf').click(function (e) {
        var query = "select * from dscn";
        var format = $('#DRpformat').val();
        $('#myframe').attr('src', 'PdfProcess.aspx?id=' + query + '&format=' + format)
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 3:36 PM

All replies

  • User632428103 posted

    Hello klbaiju,

    well ...

    a) first put a break in method pdf (code behind) and tell us it you can debug if yes there is a problem here

    else you have a problem with your call ajax..

    b) have you an error on the screen ? did you debug developer tool (F 12) ?

    i have some little pratice with $.ajax and i think you call jquery is not correct

    hope this help

    Thursday, February 11, 2016 1:52 PM
  • User475983607 posted

    Don't use AJAX.  Simply set the src attribute of an iFrame to the aspx page with whatever querystring elements required to produce the report.

    Thursday, February 11, 2016 1:59 PM
  • User1839056048 posted

    how to use that can you some example

    Thursday, February 11, 2016 2:19 PM
  • User475983607 posted

    how to use that can you some example

    Like this...

    $('#btnpdf').click(function (e) {
        var query = "select * from dscn";
        var format = $('#DRpformat').val();
        $('#myframe').attr('src', 'PdfProcess.aspx?id=' + query + '&format=' + format)
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 11, 2016 3:36 PM