Ankur
Ankur

Reputation: 1021

Binding datatable to an rdlc report

I have used Data set to bind my rdlc reports using my Stored Procedure. Can I bind a manipulated data table to my rdlc report instead of Data set(.xsd).

Upvotes: 0

Views: 39352

Answers (2)

meda
meda

Reputation: 45490

Your reportViewer on ASPX

<rsweb:ReportViewer ID="ReportViewer1" runat="server" SizeToReportContent="True">
</rsweb:ReportViewer>

Method to get data set

private DataTable GetSPResult()
{
    DataTable ResultsTable = new DataTable();

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);

    try
    {
        SqlCommand cmd = new SqlCommand("yourStorePorcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", 1);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ResultsTable);
    }

    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }

    return ResultsTable;
}

Bind result to reportviewer

DataTable dt = GetSPResult();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));

Add DataSet to your Project, then add a tableadapter to it:

enter image description here

Go through the wizard and create the entity using the store procedure

Then Make sure the DataSet file is added to your rdlc

enter image description here

Now you need to design , and add the fields or it will be blank

Upvotes: 6

RAJESH KUMAR
RAJESH KUMAR

Reputation: 517

Yes we can use manipulated datatable as datasource to report.but the field name should be same.

DataTable   dtReportData="yourdata source"

YourReportViewer.LocalReport.DataSources.Clear();
ReportDataSource RDS1 = new ReportDataSource("SampleReport", dtReportData);
YourReportViewer.ProcessingMode = ProcessingMode.Local;
YourReportViewer.LocalReport.EnableExternalImages = true;
YourReportViewer.LocalReport.ReportEmbeddedResource = "Your Report Path";
YourReportViewer.LocalReport.DataSources.Add(RDS1);

Upvotes: 4

Related Questions