Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Report Builder 2.0
Message
From
10/09/2009 10:42:18
 
 
To
09/09/2009 12:56:34
General information
Forum:
Microsoft SQL Server
Category:
Reporting Services
Miscellaneous
Thread ID:
01423230
Message ID:
01423444
Views:
67
>Just to be more specific:
>
>Say I have three tables. I want to join them to display different fields from each one on a report.
>
>Is there a way I can programatically put all these into a dataset to be linked to the ReportViewer? I found that the wizard will only let me select one table and then will set this selection in its properties. The language would be C#.

You have to change your thinking a little. I have done a mockup using 4 different datasources (a sql join, a list manually created, a dataset, and from an entity class) all on the same report. It's pretty easy really. You can add multiple datasources to your report using LocalReport.DataSources.Add().

If you want to push data to the report at runtime, it's important to have a datasource with the same name and structure created before attempting to 'add it.' Create the data using the datasources option in vs during report design time so you can use them for designing your report. Then just create the same datasources in code and push them to the report at runtime. There is a simple example which shows how to do that with one datasource here:

http://arcanecode.com/2009/02/09/using-a-local-reporting-services-2008-report-with-an-adonet-data-set/

In that example, the report actually uses datasources that were created for the report. However, I could create a datasource when designing the report, but actually push different data from the app at runtime if I wanted to. I do that when the datasource is generated from entity classes. Another example would be using a list. I could create a datasource when designing the report called employees which is populated from a select statement against a sqlserver backend (just to use for design purposes) and then push it a different datasource at runtime as long as the name and structure of the datasources expected match:

Let's say I had a class like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSRSMockup
{
    public class TestReportInfo
    {
        public TestReportInfo(string cEmpName, decimal nSalary, string cNotes)
        {
            this.EmpName = cEmpName;
            this.Salary = nSalary;
            this.Notes = cNotes;
        }
        private string _EmpName;
        public string EmpName
        {
            get { return _EmpName; }
            set { _EmpName = value; }
        }
        private decimal _Salary;
        public decimal Salary
        {
            get { return _Salary; }
            set { _Salary = value; }
        }
        private string _Notes;
        public string Notes
        {
            get { return _Notes; }
            set { _Notes = value; }
        }
    }
}
I can populate it in code like so and push it to the report as a 2nd datasource:
                    //Create a 1st dataSource for the report
                    //Create a list
                    List<TestReportInfo> oList = new List<TestReportInfo>();
                    //Populate the list based on the class TestReportInfo
                    oList.Add(new TestReportInfo("John Smith", 90000,"Notes"));
                    oList.Add(new TestReportInfo("Stacy Keach", 95000,"Notes"));
                    oList.Add(new TestReportInfo("Michael Ranes", 99000,"Notes"));

                    //Create a datatable from the list olist
                    DataTable odt = new DataTable();
                    //ListToDataTable function Added below
                    odt = ListToDataTable(oList);  //this is just a function that puts the list into a datatable to push it to a report as a demo
                    ReportDataSource odtReportData = new ReportDataSource("SSRSMockup_TestReportInfo", odt);
                    reportViewer.LocalReport.DataSources.Add(odtReportData);
I can then add another datasource created on the fly (as long as a datasource with the same name and structure was used in the designing of the report - you can use multiple datasources in a report):
                  // Add the Employee data source as a 2nd datasource
                  reportViewer.LocalReport.DataSources.Add(GetEmployees(dialog.Parameters[0]));
                 // This example just goes through different layers to generate a list of employees from an entity class - generate the list anyway you want
   private ReportDataSource GetEmployees(Microsoft.Reporting.WinForms.ReportParameter customers)
        {
            string criteria = null;
            MyCompany.Persistence.Factories.Common.EmployeeFactory oFactory = new MyCompany.Persistence.Factories.Common.EmployeeFactory();
            List<EmployeeEntity> oList = new List<EmployeeEntity>();
            IDataReader rdr = null;
            MyCompany.DataAccess.SQLProcedure oSQLProcedure = new SQLProcedure(SQLProcedure.DB.Data);
            string lcSQLString = "select cast(empl_id as varchar(15)) as idkey, * from employee where elname like 'G%' order by elname, efname";
            if (lcSQLString.Length > 0)
            {
                oSQLProcedure.SelectRecord(lcSQLString, ref rdr);
                Dictionary<string, bool> dcFields = oSQLProcedure.GetExistFields(rdr);
                while (rdr.Read())
                {
                    oList.Add(oFactory.CreateEntity(rdr, dcFields));  // this is just stuff from different layers that creates an entity object
                }
            }

            //The name of the datasource MUST match the name configured at design time see designer.cs
            //     from designer.cs: reportDataSource1.Name = "MyCompany_Entities_Common_EmployeeEntity";
            //     from designer.cs: reportDataSource1.Value = this.employeeEntityBindingSource;
            return new ReportDataSource("MyCompany_Entities_Common_EmployeeEntity", oList);
        }
and now I would have 2 datasources for the report. You must push all of the datasources used in the designing of the report. If your report requires 3 datasources, then 3 datasources should be added in code and they must match the name and structure of those used in the designing of the report. Hope that helps.

The easiest way (IMHO) to do this is to design and code the report using Visual Studio.
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform