我想将我的gridview表转换为pdf

时间:2018-04-06 06:35:43

标签: ajax asp.net-web-api

**当我试图将我的网格视图数据转换为PDF时,我得到了空引用异常网格视图标题行但数据在网格视图中可用。请帮助我解决此错误。我在这使用asp.net web API ..我试图在另一个网格视图绑定数据..它显示相同的错误空引用异常,但当我尝试直接按钮单击它工作正常..只是它显示错误当我尝试使用Web API时..请帮我解决这个问题 我已将我的编程附加到数据加载和绑定

之下
public void GetData()

        {

            string conn = ConfigurationManager.ConnectionStrings["con"].ToString();
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from api", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "api");
            //DataTable dta = new DataTable();
            //dta = ds.Tables["api"];
            //da.Fill(dta);
            GridView2.DataSource = ds;
            GridView2.DataBind();


        }


convert to pdf

 public string Button3_Click()
        {

            PdfPTable pdfTable = new PdfPTable(GridView2.HeaderRow.Cells.Count);

            foreach (TableCell headercell in GridView2.HeaderRow.Cells)
            {
                iTextSharp.text.Font font = new iTextSharp.text.Font();
                font.Color = new Basecolor(GridView2.HeaderRow.ForeColor);

                PdfPCell pdfcell = new PdfPCell(new Phrase(headercell.Text, font));
                pdfcell.BackgroundColor = new Basecolor(GridView2.HeaderStyle.BackColor);
                pdfTable.AddCell(pdfcell);
            }
            foreach (GridViewRow gridviewrow in GridView2.Rows)
            {
                foreach (TableCell tablecell in gridviewrow.Cells)
                {

                    iTextSharp.text.Font font = new iTextSharp.text.Font();
                    font.Color = new Basecolor(GridView2.RowStyle.ForeColor);

                    PdfPCell pdfcell = new PdfPCell(new Phrase(tablecell.Text));
                    pdfcell.BackgroundColor = new Basecolor(GridView2.RowStyle.BackColor);
                    pdfTable.AddCell(pdfcell);
                }
            }
            Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
            PdfWriter.GetInstance(pdfdoc, Response.OutputStream);

            pdfdoc.Open();
            pdfdoc.Add(pdfTable);
            pdfdoc.Close();

            Response.ContentType = "application/pdf";
            Response.AppendHeader("content-disposition", "attachment;filename=Employyes.pdf");
            Response.Write(pdfdoc);
            Response.Flush();
            Response.End();

            return "success";

        }

ajax function call

  $(document).ready(function () {
            $("#Button2").click(function () {
                alert("ajax call");
                $.ajax
                ({

                    method: "GET",
                    url: "api/Employees",

                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("success");
                    },

                    error: function (responce) {
                        alert("error");
                    },


                });
            });
        });
 <asp:Button ID="Button1" runat="server" Style="width: 136px" Text="Export to Excel" Width="175px" />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
          <asp:Button ID="Button2" runat="server"  Text="Export to PDF" Width="175px" />
        <br />
        <br />
        <div class="auto-style1" id="NewData">
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true">
            </asp:GridView>
        </div>
here is my controller class
 public class EmployeesController : ApiController
    {
       // success s = new success();

        BusinessLogicLayer bal = new BusinessLogicLayer();

        public string Get(string username, string password)

        {
            return bal.login(username, password);
        }


        [System.Web.Mvc.HttpPost]
        public string POST(string firstname, string lastname, string username, string password, string email)
        {
            return bal.Register(firstname, lastname, username, password, email);
        }
         [System.Web.Http.HttpGet]
           public string buttonclick()
           {
              // MessageBox.Show("come");
               return bal.ExportConvert();

        }

      here is my business logic layer class method

 public string ExportConvert()
        {
            success s = new success();

            return s.Button3_Click();

        }[enter image description here][1]

这是我的登录页面代码

**<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body style="padding-top:20px;">
<div class="col-md-10 col-md-offset-1">

    <div class="well">
        <table class="table table-bordered">
            <thead>
                <tr class="success">



                        <th colspan="2">
                            Existing User Login
                    <a class="btn btn-success pull-right" href="Registration.html">Register</a>
                            </th>


                </tr>
            </thead>
            <tbody>



                <tr>
                    <td>UserName</td>
                    <td>
                        <input type="text" id="username"   placeholder="username"/>
                    </td>
                </tr>

                <tr>
                    <td>Password</td>
                    <td>
                        <input type="password" id="password"   placeholder="Password" />
                    </td>
                </tr>
                <tr class="success">

                    <td colspan="2">
                        <input type="button" id="btnlogin"  class="btn btn-success" value="login" />
                    </td>
                </tr>

            </tbody>

        </table>

        <div class="modal fade" tabindex="-1" id="successmodel"
             data-keyboard="false" data-backdrop="static">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">

                    <button type="button" class="close" data-dismiss="modal">
                        &times;
                    </button>
                    <h4>Success</h4>
                </div>
                <div class="modal-body">
                    <h2>Registraton success</h2>

                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" class="btn btn-success">
                        Close

                    </button>

                </div>
                </div>
            </div>

        </div>

    <div id="divError" class="alert alert-danger collapse">
        <a id="linkClose" class="close" href="#" >&times;</a>
        <div id="divErrorText"></div>
    </div> 


    </div>

    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
    <script type="text/javascript" >

        $(document).ready(function () {
            $('#linkClose').click(function () {
                $('divError').hide('fade');
            })


        });

        $('#btnlogin').click(function () {
            $.ajax({

                url: '/api/Employees/?username=' + $("#username").val() + '&password=' + $("#password").val(),
                method: 'Get',
                contentType: 'application/json',
                data: { id: 1 },
                dataType: 'json',

                success: function (data) {
                    console.log(data);
                   //var data = data.d;
                   //alert("success " + data);
                   if (data == 'success') {
                       window.location = "/success.aspx";
                   }
                   else {
                       alert("Wrong Username and Password");
                   }
                  /*  if (data != null) {
                        $.each(data, function (i, obj) {
                            var c = new WorkflowsEntity(data[i]);    //logic
                            self.WorkflowList.push;
                        });
                        alert("success");
                        //window.location.href = "/success.html";
                    }*/
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(" un correct username password ");
                    $('#divErrorText').text(jqXHR.responceText);
                    $('#divError').show('fade');
                }
            });
        });


    </script>

</body>
</html>**

0 个答案:

没有答案