Export PDF with gridview asp.net c #

0

Hello, I would like you to help me. I am doing an export of a gridview to a pdf in the asp.net c #

language

but I get an error that is missing the reference that should have runat server but if the datagrid is within a form server = runat

This is my CodeBehind code:

protected void Page_Load(object sender, EventArgs e)
{
     txtFechaDesembolso.Attributes.Add("readonly", "readonly");

    if (!IsPostBack)
    {


        SqlConnection con2 = new SqlConnection("server=111.222.333.444\SQL2012;    database=DBCredito;      user id=sa;                   password=*********;");
        //SqlConnection con2 = new SqlConnection("server=111.222.333.444;    database=DB; user id=sa; password=**********;");
        SqlCommand cmd2 = new SqlCommand("select idproducto,descripcion from credito..producto where CodigoProducto in('MICROCRÉDITO ','Consumo','OLLAORO','AGRICOLA')", con2);
        SqlDataAdapter sda2 = new SqlDataAdapter(cmd2);
        DataSet ds2 = new DataSet();
        sda2.Fill(ds2);
        drptipocredito.DataSource = ds2;
        drptipocredito.DataTextField = "descripcion";
        drptipocredito.DataValueField = "idproducto";
        drptipocredito.DataBind();
    }
}
  private string ObtenerCadenaConexion()
{
    var cadenaCx = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    return Crypto.Decrypt(cadenaCx, "qapaq15926*");
}

  private void ExportGridToPDF()
  {

      Response.ContentType = "application/pdf";
      Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.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();
      GridView1.AllowPaging = true;
      GridView1.DataBind();  
  }  




protected void btnCalcular_Click(object sender, EventArgs e)
{
    if (txtImporteCredito.Text == "")
    {
        Response.Write("<script>alert('Ingrese el Importe de Crédito');</script>");
    }
    else if (txtFechaDesembolso.Text == "")
    {
        Response.Write("<script>alert('Ingrese la Fecha de Desembolso');</script>");

    }
    else if (txtTEA.Text == "")
    {
        Response.Write("<script>alert('Ingrese TEA');</script>");

    }
    else
    {
        ServiceReference1.SvcCreditoExternoClient tabla = new ServiceReference1.SvcCreditoExternoClient();
        ServiceReference1.DtoGeneradorTablaAmortizacionRequest dtorequest = new ServiceReference1.DtoGeneradorTablaAmortizacionRequest();
        ServiceReference1.DtoDividendo dtodividendo = new ServiceReference1.DtoDividendo();
        List<ServiceReference1.DtoDividendo> listadtodividendo;
        dtorequest.AjustarTabla = ServiceReference1.EnumeradosConfiguracionNegocioMetodosAjusteTabla.SinAjuste;

        dtorequest.AplicaAlgoritmoRedondeoCuota = true;
        dtorequest.AplicaDiaLaborable = true;
        dtorequest.AplicaDiaLaborableFinSemanaAdelante = true;
        dtorequest.AplicaFechasIguales = true;
        dtorequest.AplicaFindeMes = true;
        dtorequest.AplicaPrimerVencimiento = true;
        dtorequest.BaseCalculo = 360;
        dtorequest.CodigoSectorOrganismoControl = 0;
        dtorequest.Decimales = 2;
        dtorequest.DecimalesCuota = 2;
        dtorequest.DecimalesTasa = 2;
        dtorequest.EsExento = false;
        dtorequest.FactorDeRedondeo = ServiceReference1.EnumeradosConfiguracionNegocioFactorRedondeo.SnDecimales;
        dtorequest.Fecha1erVencimiento = Convert.ToDateTime(txtFechaDesembolso.Text);//Convert.ToDateTime("2016-07-06");
        dtorequest.FechaInicioTabla = Convert.ToDateTime("2016-05-29");
        //FormaPago = 12,
        dtorequest.GeneraTasaObjetivo = false;
        dtorequest.Gradiente = 0;
        dtorequest.Idproducto = 1;
        dtorequest.Institucion = 1;
        //ListaRubroDiferido = Rubrio.Add(pag),
        dtorequest.Monto = Convert.ToDecimal(txtImporteCredito.Text);//Convert.ToDecimal(1900.00);
        dtorequest.MontoCapitalFijo = 0;
        dtorequest.MontoTIR2 = 10000;
        dtorequest.MontoTIR3 = 0;
        int somestring = Convert.ToInt32(dprNumerocuota.SelectedItem.Value);
        dtorequest.NumeroCuotas = Convert.ToInt32(dprNumerocuota.SelectedItem.Text.Substring(0, 2));// 12;
        dtorequest.Oficina = 5;
        dtorequest.Periodicidad = 30;
        dtorequest.PeriodicidadCapital = 0;
        dtorequest.PeriodicidadInteres = 0;
        dtorequest.PeriodosGracia = 0;
        dtorequest.Plazo = 360;

        Decimal FactorMaximo;
        FactorMaximo = Convert.ToDecimal(150000);

        Decimal TasaMensual;
        TasaMensual = Convert.ToDecimal(0.08);

        Decimal TasaDiaria;
        TasaDiaria = Convert.ToDecimal((TasaMensual / 30) / 100);

        Decimal factor1, factor2;
        factor1 = (Convert.ToDecimal(dtorequest.Monto) * Convert.ToInt32(dtorequest.Plazo) * Convert.ToDecimal(TasaDiaria)) + Convert.ToDecimal(dtorequest.Monto);

        factor2 = Convert.ToDecimal(factor1) * Convert.ToInt32(dtorequest.Plazo) * Convert.ToDecimal(TasaDiaria);

        Decimal SeguroDesgravamen;
        SeguroDesgravamen = (Convert.ToDecimal(factor1) > Convert.ToDecimal(FactorMaximo) ? Convert.ToDecimal(FactorMaximo) : Convert.ToDecimal(factor2));

        Decimal MontoFinanciado;
        MontoFinanciado = Math.Round(Convert.ToDecimal(dtorequest.Monto) + Convert.ToDecimal(SeguroDesgravamen), 2);

        Label1.Text = Convert.ToString(MontoFinanciado);

        dtorequest.PorcentajeMinimoCuoton = 0;
        dtorequest.RedondeoHacia = ServiceReference1.EnumeradosConfiguracionNegocioTipoRedondeo.Arriba;

        dtorequest.Tasa = Convert.ToDecimal(txtTEA.Text);//40;
        dtorequest.TasaInicialObjetivo = 0;
        dtorequest.TipoGracia = ServiceReference1.EnumeradosConfiguracionNegocioTipoGracia.GraciaCapital;
        dtorequest.TipoTabla = "CUOTFIJA";

        listadtodividendo = tabla.ConsultarTablaAmortizacion(dtorequest).ToList();

        GridView1.DataSource = listadtodividendo;


        GridView1.DataBind();

        // GridView1.Columns[1].Visible = false;


    }
}

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{




      e.Row.Cells[0].Visible = false;
      e.Row.Cells[1].Visible = false;
         e.Row.Cells[3].Visible = false;
      e.Row.Cells[5].Visible = false;
      e.Row.Cells[6].Visible = false;
      e.Row.Cells[7].Visible = false;
      e.Row.Cells[8].Visible = false;
      e.Row.Cells[10].Visible = false;
      e.Row.Cells[12].Visible = false;
       e.Row.Cells[14].Visible = false;
      e.Row.Cells[15].Visible = false;
       e.Row.Cells[16].Visible = false;
        e.Row.Cells[18].Visible = false;
        e.Row.Cells[19].Visible = false;
        e.Row.Cells[20].Visible = false;
       e.Row.Cells[21].Visible = false;
       e.Row.Cells[22].Visible = false;
      e.Row.Cells[23].Visible = false;



}

    protected void btnPdf_Click(object sender, EventArgs e)
    {
        ExportGridToPDF();  
    }
}

asp.net

    <form id="form1" runat="server">
           <asp:GridView ID="GridView1" runat="server" OnRowCreated="GridView1_RowCreated"></asp:GridView>
         <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="true">
                                </cc1:ToolkitScriptManager>
    <div class="contenido">
<img src="img/LOGO%20QAPAQ%202%20S%203.png"  style="float:left; background:#fff;padding-top:20px; padding-left:10px; width:600px"/>


        <div class="container">
            <div class="row"> 
                 <div class="col-sm-10 col-md-10">
                   <br />  <br />  <br />
                      <h1>  <strong id="letra"> Simulador de Créditos </strong></h1>

                </div> 
            </div> <br /> <br />
            <div class="row">
                <div class="col-sm-10 col-md-10">
                    <table class="table">
                        <tr>
                            <td>Tipo de Crédito:</td>
                             <td><asp:DropDownList ID="drptipocredito" runat="server" CssClass="form-control">
                                 <%-- <asp:ListItem Text="Microempresa" Value="0" />
                                   <asp:ListItem Text="Consumo" Value="1" />
                                   <asp:ListItem Text="Olla de Oro" Value="2" />
                                   <asp:ListItem Text="Agrícola" Value="3" />--%>
                                 </asp:DropDownList>

                             </td>
                        </tr>

                        <tr>
                            <td>Tipo Moneda:</td>
                             <td><asp:DropDownList ID="DropDownList1" runat="server" CssClass="form-control">
                                  <asp:ListItem Text="Soles" Value="0" />
                                   <asp:ListItem Text="Dolares" Value="1" />
                                 </asp:DropDownList>

                             </td>
                        </tr>
                        <tr>
                            <td>Importe del crédito:</td>
                             <td><asp:TextBox ID="txtImporteCredito" runat="server" CssClass="form-control" placeholder="Ingrese el Importe de Crédito"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td>Número de cuotas</td>
                              <td>
                                  <asp:DropDownList ID="dprNumerocuota" runat="server" CssClass="form-control">
                                  <asp:ListItem Text="6 Cuotas" Value="0" />
                                   <asp:ListItem Text="12 Cuotas" Value="1" />
                                       <asp:ListItem Text="18 Cuotas" Value="2" />
                                        <asp:ListItem Text="24 Cuotas" Value="3" />
                                      <asp:ListItem Text="36 Cuotas" Value="4" />

                                 </asp:DropDownList>

                              </td>
                        </tr>
                        <tr>
                            <td>Fecha de Desembolso:</td>
                            <td><asp:TextBox ID="txtFechaDesembolso" runat="server" CssClass="form-control"></asp:TextBox>
                                 <cc1:CalendarExtender ID="CalendarExtender2" PopupButtonID="txtFechaDesembolso" runat="server" TargetControlID="txtFechaDesembolso" Format="dd/MM/yyyy"> </cc1:CalendarExtender> 
                            </td>
                        </tr>
                        <tr>
                            <td>Seguro de desgravamen:</td>
                              <td>
                                   <asp:DropDownList ID="DropDownList4" runat="server" CssClass="form-control">

                                   <asp:ListItem Text="Con Seguro" Value="0" />


                                 </asp:DropDownList>
                              </td>
                        </tr>
                        <tr>
                            <td>TEA:</td>
                             <td><asp:TextBox ID="txtTEA" runat="server" CssClass="form-control" placeholder="Ingrese el TEA"></asp:TextBox>
                                  <asp:RegularExpressionValidator ID="RegexDecimal" runat="server" ValidationExpression="((\d+)((\.\d{1,2})?))$" ErrorMessage="Ingrese un monto decimal"  ControlToValidate="txtTEA" />
  <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" />
                             </td>
                        </tr>

                        <tr>

                            <td> 
                            <asp:Button ID="btnCalcular" runat="server" Text="Calcular"  class="btn btn-danger" OnClick="btnCalcular_Click" ></asp:Button>
                   </td> 
                            <td>
                                  <asp:Button ID="btnPdf" runat="server" Text="PDF"  class="btn btn-danger" OnClick="btnPdf_Click" ></asp:Button>
                            </td><br /> <br />
                        </tr>

                    </table>
                </div>

            </div>

        </div>
    </div><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </form>
    
asked by PieroDev 13.06.2017 в 23:57
source

1 answer

0

In order to generate the PDF we must also add

 public override void VerifyRenderingInServerForm(Control control)
            {
                /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
                   server control at run time. */
            }
    
answered by 14.06.2017 в 00:52