alen antony
alen antony

Reputation: 35

How to show report in windows forms through coding in C#?

I am new to Report. I am using MicrosoftaReportViewr to show report.For that I create and filled dataset. Then I try to give that dataset as source for reportviewr1. But I didn't get the actual coding.My code is

if (cbReprt.Text == "FirmDetails")
{
            ReportDocument rpt = new ReportDocument();

rpt.Load("C:\\Users\\ALEN\\Desktop\\Merchant\\MerchantAssociation\\MerchantAssociation\\CrystalReport1.rpt");

SqlConnection myCon;
SqlDataAdapter myAdapter;

DataSet1 myDataset = new DataSet1(); //The DataSet you created.

           myCon = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");

           SqlCommand cmd3 = new SqlCommand("select * from View1", myCon);
           cmd3.CommandType = CommandType.StoredProcedure;

           myAdapter = new SqlDataAdapter(cmd3);
           myAdapter.Fill(myDataset, "View1");
           rpt.SetDataSource(myDataset);
           reportViewer1.LocalReport.DataSources= rpt;
          reportViewer1.RefreshReport();
        }

But I reportViewer1.LocalReport.DataSources= rpt; showing error... any special namespace is needed or not?

Upvotes: 0

Views: 4589

Answers (1)

Vedran
Vedran

Reputation: 11029

This is how you can use DataSources for the reportviewer control:

var myRds = new ReportDataSource("NameOfDataSourceDefinedInTheReport", myDataSet);
reportViewer1.LocalReport.DataSources.Add(myRds);

Keep in mind that the report has to be in SSRS format, and if you're using the ReportViewer control 2010 - it has to be the SSRS 2008 or later format (2005 is deprecated).

The name 'NameOfDataSourceDefinedInTheReport' has to be the same as defined in the actual report for any data to be loaded, and of course the structure of the data has to be the same as defined in the report.

Upvotes: 1

Related Questions