With ASP.NET and ActiveReports, you can set up a Web Service that returns a report document which can be shown in a report viewer control.
This walkthrough illustrates how to create a Web Service that returns the contents of an ActiveReport as a byte array.
This walkthrough is split up into the following activities:
- Creating an ASP.NET Web Service project
- Adding a report and connecting it to data
- Adding controls to the report
- Adding code to create the Web Method
- Testing the Web Service
- Publishing the Web Service
- Creating a virtual directory in IIS
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit operating Windows system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).
You must also add a reference in your project to the System.Data.OleDb namespace.
When you have completed this walkthrough, you will have a Web Service that returns the contents of an ActiveReport as a byte array.
To create an ASP.NET Web Service project
- From the Visual Studio File menu, select New, then Web Site.
- In the Templates window of the New Web Site dialog, select ASP.NET Web Service.
- Change the name of the project from WebSite1 to ARDocumentWS.
- Click OK to open the new project in Visual Studio.
To add a report and connect it to data
- From the Website menu, select Add New Item.
- Select ActiveReports 6 (code-based) File, rename it rptProducts, and click the Add button.
- In the message box that appears, click the Yes button to place the report inside the App_Code folder. Instead of the design view, the code view of the report opens.
- To go to the design view of the report, in the Solution Explorer, right-click rptProducts and select View Designer.
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the OLE DB tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
- Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter the following SQL query
SQL Query |
Copy Code |
SELECT * FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID ORDER BY CategoryName |
- Click OK to save the data source and return to the report design surface.
To add controls to the report
- Right-click on the design surface of the report and select Insert, then Group Header/Footer to add a group header and group footer section.
- Click to select the group header section, and in the Properties window, make the following changes:
- Change the Name property to ghCategories
- Change the DataField property to CategoryName
- Change the BackColor property to LightGray
- In the Report Explorer, expand the Fields node, then the Bound node.
- Drag the following field onto ghCategories and set the properties as indicated.
Field for ghCategories
Field |
Size |
Location |
Miscellaneous |
CategoryName |
6.5, 0.2 in |
0, 0 in |
Font size = 14 ForeColor = DarkGreen |
- Add a second GroupHeader/Footer section to the report to contain labels.
- Make the following changes to the new group header:
- Change the Name property to ghProducts
- Change the BackColor property to WhiteSmoke
- Add labels with the following properties to ghProducts:
Labels for ghProducts
Name |
Text |
Location |
lblProductName |
Product Name |
0, 0 in |
lblUnitPrice |
Unit Price |
2.4, 0 in |
lblUnitsInStock |
Units in Stock |
4, 0 in |
lblUnitsOnOrder |
Units on Order |
5.5, 0 in |
- Set the CanShrink property of the detail section to True.
- From the Report Explorer, drag the following fields onto the detail section and set the properties as indicated.
Controls for the detail section
Field |
Text |
Miscellaneous |
Location |
ProductName |
Product Name |
Size = 2.25, 0.2 in |
0, 0 in |
UnitPrice |
Unit Price |
OutputFormat = Currency Alignment = Right |
2.4, 0 in |
UnitsInStock |
Units In Stock |
Alignment = Right |
4, 0 in |
UnitsOnOrder |
Units On Order |
Alignment = Right |
5.5, 0 |
To write the code to create the Web Method
- On the Service.vb or Service.cs tab is the code view of the Service.asmx file.
- Replace the existing WebMethod and HelloWorld function with the following code.
To write the code in Visual Basic.NET
Visual Basic.NET code. REPLACE the existing WebMethod and function with this code. |
Copy Code |
<WebMethod( _
Description:="Returns a products report grouped by category")> _
Public Function GetProductsReport() As Byte()
Dim rpt As New rptProducts()
rpt.Run()
Return rpt.Document.Content
End Function
|
To write the code in C#
C# code. REPLACE the existing WebMethod and function with this code. |
Copy Code |
[WebMethod(Description="Returns a products report grouped by category")]
public Byte[] GetProductsReport()
{
rptProducts rpt = new rptProducts();
rpt.Run();
return rpt.Document.Content;
}
|
To test the Web Service
- Press F5 to run the project. The Service page appears in your browser.
- In the list of supported operations at the top, click GetProductsReport.
- Click the Invoke button to test the Web Service operation.
- If the test is successful, you will see the binary version of the contents of rptProducts.
To publish the Web Service
- In the Solution Explorer, right-click the project name and select Publish Web Site.
- Click the OK button. The Web Service is now available for consumption by other projects.
To create a virtual directory in Internet Information Services
- In Windows Explorer, navigate to the folder containing your Web service folder.
- Right-click the folder and select Sharing and Security.
- On the Web Sharing tab, select the Share this folder radio button.
- Click the OK button to save the setting and close the window.
To create a virtual directory in IIS 7.x
- In the Control Panel, open Administrative Tools, then Internet Information Services Manager.
- In the Internet Information Services Manager window that appears, expand the tree view in the left pane until you see the Web Site you need to create a virtual directory for.
- Right-click the node for the Web Site and select Add Application…
-
In the Add Application dialog box that appears, enter the following information:
Alias: enter a name for the virtual directory.
Physical path: select the folder that contains your Web service folder.
- Click the OK button to save the settings and close the window.
|
Important: In order to consume Web services in your Windows applications, you must set permissions to allow the ASP.NET user to consume the services. Ask your server administrator for help with this. |
For information on consuming the Document Web Service in a viewer, see Document Windows Application.
See Also