Making Server Side Report Modifications
The last section showed how the client is limited to using the viewer control for making runtime modifications. This doesn’t give you many options for making runtime modifications. But that doesn’t mean that it is impossible to make changes using the ReportDocument class. You just have to do it on the server within the web service application.
Making changes on the server side is very powerful because you get full access to the ReportDocument class. You can make changes to the report objects so that the report is completely dynamic. And you don’t have to learn any new coding practices because you can use same code shown throughout this book. The only difference is how you reference the ReportDocument object. That is the tricky part!
Before showing you how to make server side modifications, you need to understand that there is one major problem with this approach. The server can’t communicate with the client because the client can’t pass information to the report web service. If this were a typical application, the user would enter information on a form and this information is used to modify the ReportDocument object prior to printing the report. But you can’t do this with report web services. If you try to pass information on the query string then the web service returns an error. You also can’t use cookies because these are strictly client based. You are restricted to making changes to the ReportDocument object that aren’t user specific.
The one trick around this is to use multiple web services that open the same report. Each web service can perform a different modification to the report object prior to previewing it. For example you could have one web service show the default report and another web service modifies the default grouping of a report. This lets one report be used by different web services and the end user thinks that they are looking at different reports. As another example you could also have different web services specific to individual clients. Each client gets the same report, but you can charge them an additional fee if they want it customized to match their corporate image.
Modifying the ReportDocument object is done within the CreateReport() method of the web service. The CreateReport() method declares and instantiates the report object.
The CreateReport() method is within the web service’s underlying .asmx.vb file (.asmx.cs for C# programs). By default the .asmx.vb file is hidden from the IDE. To see this file click on the menu option Project | Show All Files. Go to the Solution Explorer window and find the web service file (it has the .asmx extension). Click on the + sign to the left of it. This lists the underlying .asmx.vb file for the web service. Right-click on this .asmx.vb file and select View Code. Scroll through the code till you find the method CreateReport(). It’s probably near the end.
Here is the CreateReport() method code for the EmployeeList web service report that was created earlier in this chapter.
Protected Overridable Function CreateReport() As ReportDocument _
Implements ICachedReport.CreateReport
Dim report As EmployeeList = New EmployeeList
AddHandler report.InitReport, New EventHandler( _
AddressOf Me.webService.OnInitReport)
Return report
End Function
The important part of this code is that it declares and instantiates a report object variable of the EmployeeList class. Then it returns this report object to the calling method. All you have to do is put your changes after the report variable is instantiated. Any of the code in this book can be put here and it will work without changes.
Being able to modify the ReportDocument on the server is crucial if you have a report based on a DataSet. If you recall from Chapter 17, datasets have to be manually populated by the application and assigned to the report using the SetDataSource() method of the ReportDocument object. You also know from the previous section that the viewer class can only set login credentials for a report. It can’t set the data source property. If a report uses a dataset and you make it into a web service, the viewer it’s going to give you an error. To make it work you have to set the report’s data source within the CreateReport() method of the web service. Listing 18-5 shows how this is done.
Protected Overridable Function CreateReport() As ReportDocument _
Implements ICachedReport.CreateReport
Dim report As EmployeeList = New EmployeeList
AddHandler report.InitReport, New EventHandler(AddressOf
Me.webService.OnInitReport)
'Put all your report modification code here
Dim MyDataSet as New DataSet
'Call one of the sample methods from Chapter 17 for populating
‘the DataSet object
FillDataSet(MyDataSet)
Report.SetDataSource(MyDataSet)
Return report
End Function
At this point in the book, this code shouldn’t need much explanation. After declaring the report variable, the MyDataSet variable is declared and instantiated. The method FillDataSet() is called to populate the MyDataSet variable. This method can be any of the sample methods from Chapter 17 that populate a DataSet object. After MyDataSet has data, it is assigned to the report using the SetDataSource() method. A viewer control can now call this web service and display the report without getting an error.