load crystal reports with gridview data? [duplicate]

0

I have a gridview in asp .net filled with a datatable and I need to click on a print button to generate a report crystal reports with the gridview data. I was investigating and apparently it is with a "typed dataset", I do not understand very well how to do it.

So I have filled my gridview

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from table where cod_user='"+ Session["Cod_user"]+"'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gridview1.Visible = true;
gridview1.DataSource = dt;
gridview1.DataBind();
con.Close();

Now I'm supposed to generate a report with those data by clicking on a button.

  

I use webform.aspx with c #

    
asked by sunflower 14.03.2018 в 19:20
source

1 answer

0

I have what you need from an old application, it is visual basic, but it gives you the logic of what you should do:

        Dim parametro As New ParameterValues
        Dim tipo As New ParameterDiscreteValue
        Dim reporte As New rpt_puestos
        Dim con_base As New SqlClient.SqlConnection
        Dim adp_div As New SqlClient.SqlDataAdapter
        Dim puesto As New DataTable
        Dim sqlcadena As String
        con_base.ConnectionString = Application("cadena").ToString
        sqlcadena = "select * from tabla"

        adp_div = New SqlClient.SqlDataAdapter(sqlcadena, con_base)
        puesto = New DataTable
        adp_div.Fill(puesto)
        reporte.SetDataSource(puesto)
        puesto = New DataTable
        adp_div.Fill(puesto)
        parametro.Add(tipo)
        tipo.Value = "TITULO REPORTE"      

        puesto.Dispose()
        con_base.Dispose()
        adp_div.Dispose()
        reporte.DataDefinition.ParameterFields("titulo").ApplyCurrentValues(parametro)        
        reporte.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, tipo.Value.ToString.Replace(" ", "_"))
        Response.[End]()
        reporte.Dispose()

What you need is to give the data source of the report the table that you have already obtained from the query, if the report has other parameters such as title or something else, this is the example of how to pass them and finally the report is exported in pdf format, but this can be changed according to what is needed.

In summary: You must add the crystal reports references

You must create the report with the crystal reports designer and save it in some folder of the application

If the report has parameters you should add them in the code, if not with the only data source is enough

add the table to the datasource of the report

Export the report

(The code in c # is very similar)

    
answered by 14.03.2018 в 22:07