Sheikh Rahat Ali
Sheikh Rahat Ali

Reputation: 1333

Unable to assign a Dataset to ReportDataSource in C#

I am trying to pass a dataset as a datasource for a report which i want to view with the help of Microsft Reporting Control hosted on a winform's Form. I am using the following code for this purpose but fails in accomplishing the task.

    private void Form1_Load(object sender, EventArgs e)
    {
        // Sql Connection Object
        SqlConnection con = new SqlConnection(@"Data Source=SEVEN01-PC\SQLEXPRESS;Initial Catalog=RealWorld;Integrated Security=SSPI;");

        // Sql Command Object
        SqlCommand cmd = new SqlCommand("Select * from ProductReorder", con);

        try
        {
            // Open Connection
            con.Open();

            // Dataset Object
            DataSet ds = new DataSet();

            // Sql DataReader Object
            SqlDataReader reader = cmd.ExecuteReader();

            // Fill Data Set
            ds.Tables[0].Load(reader);

            // Close Sql Datareader and Connection Objects
            reader.Close();
            con.Close();

            //provide local report information to viewer
            reportViewer1.LocalReport.ReportEmbeddedResource = "ProductReorder.rptProductReorder.rdlc";

            //prepare report data source
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "dsProductReorder_dtProductReorder";
            rds.Value = ds.Tables[0];
            reportViewer1.LocalReport.DataSources.Add(rds);

            //load report viewer
            this.reportViewer1.RefreshReport();
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

Any suggestions how to assign the dataset to the report datasource!

Upvotes: 1

Views: 11893

Answers (2)

nemki
nemki

Reputation: 1

ReportTableAdapter ta = new ReportTableAdapter();
var ds= ra.GetData();
ReportDataSource rd = new ReportDataSource("RepotrDS",ds.ToList());

This works for me.

Upvotes: 0

2GDev
2GDev

Reputation: 2466

Maybe you can try this...

    LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("dsProductReorder_dtProductReorder", d.Tables[0]));

Upvotes: 3

Related Questions