Partager via


Displaying WCF Data Service data in local reports

Its been sometime that I have blogged. I recently got asked by one of my peers on how to display the data from DataService on a local report and how to pass a parameter .

Its relatively easy to do this , the steps would be

  • Make a connection to the DataService using Data Service client API
  • Get the Data as a collection , my favorite List<T> :)
  • Add it to the ReportDataSource and bind it to the LocalReport

However, regarding the parameter passing , we know that if we add a parameter to the local report it does not prompt for the user and the developer is responsible for getting the value. Also you need to decide whether you need to get the entire data on the client and then do a filter on it or if you want to get the filtered data from the server based on the parameter selected from the client. I took the former approach and get the entire data(considering the small amount of data that I have ). Here is how

I am assuming that you already have a neat WCF Data Service defined and ready to take the requests, in this case at https://localhost:54560/MyDataService/NorthwindService.svc/ . If you are new to this then I suggest you take a look at Getting Started with Data Services section on MSDN

 

In order to code the client lets first create a Data Service proxy using the tool DataSvcUtil.exe . Open up Visual Studio command prompt and type the following

c:\Users\bindeshv>DataSvcUtil /out:NorthwindService.cs /language:CSharp /uri:https://localhost:54560/MyDataService/NorthwindService.svc/

 

Next go ahead and add this to your WinForms project as an existing item. Once done you need to add the Using reference to the newly added chsarp namespace.

 

Add a RDLC file to the project as shown below

image

Next we need to define the data source for the local report. In order to do that bring up the Data Sources explore (from Visual Studio Menu , Data > Show Data Sources) and then click on Add New Data Source in the Data Source Explorer. This will start a wizard and in the first window choose Service as the Data Source type and in the next window type in the url of your WCF Data Service as shown below and click Go

 

 

image

Go ahead and give a namespace and then click on OK button.  Once the service reference is added to the project and since WCF Data Services exposes entity you need to choose the object that you will bind the report to. In order to do this again go to the Data Source Explorer and then Add a new Data Source but this time choose Object instead of service and point it to the WCF Service that was added to the project like shown below

 

image

Once the object is added to the Data Source Explore you can proceed with the normal design of the report by dragging in a table component and then adding the necessary fields

image

image

Now we need to add the Report Parameter, in this case I will specify the Employee ID as input for filtering , so that we see details of only one specific employee. In the Visual Studio Menu option go ahead and chose Report > Report Parameters..   and the required parameter

image

Once the parameter is defined you need to specify it in the Reports Table Properties Filter section as shown below

 

image

Once the filtering is done go ahead and add a Report Viewer control to the form. Remember we need to device our own parameter for Reports in case of local report. Here is how my form looks after the design is done

 

image

And here is how the code behind looks like

 

        //Form level member
       private List<Employees> empList = null ;
        private void Form1_Load(object sender, EventArgs e)
        {
            if (empList == null)
            {

                NorthwindModel.NorthwindEntities entitiesContext = new NorthwindModel.NorthwindEntities(new Uri("https://localhost:54560/MyDataService/NorthwindService.svc/"));
                try
                {

                   empList = (from emps in entitiesContext.Employees
                               select emps).ToList<Employees>();
                

                    ReportDataSource rds = new ReportDataSource("DataServiceForms_NorthwindReference_Employees", empList);
                    this.reportViewer1.LocalReport.DataSources.Clear();
                    this.reportViewer1.LocalReport.DataSources.Add(rds);

                }

                    
                catch (Exception _ex)
                {
                    MessageBox.Show(_ex.Message);

                }
            }

            //get all the employee ids and bind it 

            var empIDs = from emps in empList
                         select emps.EmployeeID;
            foreach (int i in empIDs)
            {
                this.comboBox1.Items.Add(i);
            }
            //set the combo to display the first item
            this.comboBox1.SelectedIndex = 0;

            //bind parameters to the report
            BindParameter();
        }

       private void BindParameter()
       {
           ReportParameter param = new ReportParameter("EmpID", this.comboBox1.SelectedItem.ToString());
           this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { param });
           this.reportViewer1.RefreshReport();
       }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           BindParameter();
       }

The completed report looks like this

 

image

Until next post ..