Monday, 21 July 2014

Create RDLC report

No comments
1> Create new Dataset , add columns
2> Create SP
3>add references of Microsoft.ReportViewer.WebForms
4>draag and drop RDLC report

Register assambly

ASPX

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

 <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
                    Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
                    WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
                    <LocalReport ReportPath="Resolution_report.rdlc">
                        <DataSources>
                            <rsweb:ReportDataSource Name="DataSet1" />
                        </DataSources>
                   </LocalReport>
 </rsweb:ReportViewer>

CS

on button click

protected void Btndownloadpdf_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Example"].ToString());
        SqlCommand cmd = new SqlCommand("Your_SP", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        ReportDataSource datasource = new ReportDataSource("DataSet1", ds.Tables[0]);
        ReportViewer ReportViewer1 = new ReportViewer();
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
        ReportViewer1.LocalReport.Refresh();
        byte[] byt = ReportViewer1.LocalReport.Render("PDF");
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename= Report" + "." + "pdf");
        Response.OutputStream.Write(byt, 0, byt.Length); // create the file 
        Response.Flush(); // send it to the client to download 
        Response.End();
    }