Walkthrough: Saving and Loading to a Memory Stream
ActiveReports allows you to save and load a report as a memory stream. Saving a report as a memory stream makes it possible to save and load reports from a database or pass reports back and forth between DLLs.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding the viewer to Form1
- Adding code to the Form_Load event to save the report to a memory stream and load it to the viewer
To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).
When you have completed this walkthrough, you will have a report that looks similar to the following.
Adding an ActiveReport to a Visual Studio project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptMemoryStream.
- Click Open (Add in Visual Studio 2005).
Connecting the report to a data source
To connect the report to a data source
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click the Build... button.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next >>
- Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from customers ORDER BY country".
- Click OK to return to the report design surface.
Adding controls to the report to contain data
To add controls to the report
- Right-click anywhere on rptMemoryStream and select Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the name to ghCustomers
- Change the DataField property to Country
- Make the following changes to the Detail section:
- Change the BackColor property to PaleTurquoise
- Change the Height property to 0.25 in
- Add six text boxes with the following properties to rptMemoryStream (widen as needed):
DataField |
Name |
Text/Caption |
Section |
Location |
Country |
txtCountry |
Country |
ghCustomers |
0, 0 |
CustomerID |
txtCustomerID |
Customer ID |
Detail |
0, 0 |
CompanyName |
txtCompanyName |
Company Name |
Detail |
1.125, 0 |
Address |
txtAddress |
Address |
Detail |
3, 0 |
City |
txtCity |
City |
Detail |
4.375, 0 |
Phone |
txtPhone |
Phone |
Detail |
5.5, 0 |
Adding the viewer to Form1
To add the viewer to Form1
- Click on the ActiveReports viewer control in the appropriate toolbox and drag it onto Form1.
- Set the viewer control's Dock property to Fill.
Adding code to the Form1_Load event
To write the code in Visual Basic
- Right-click in any section of Form1, and select View Code to display the code view for the Windows Form. At the top left of the code view for Form1, click the drop-down arrow and select (Form1 Events). At the top right of the code window, click the drop-down arrow and select Load. This creates an event-handling method for the Form1_Load event. Add code to the handler to:
- Save the report to a memory stream
- Load the memory stream to the ActiveReports viewer
To write the code in C#
- Click at the top of Form1 to select the Windows Form. Click the events icon in the Properties window to display available events for the section. Double-click Load. This creates an event-handling method for the Form1_Load event. Add code to the handler to:
- Save the report to a memory stream
- Load the memory stream to the ActiveReports viewer
The following example shows what the code for the method looks like.
' Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim strm As New System.IO.MemoryStream()
Dim rpt As New rptMemoryStream()
rpt.Run()
rpt.Document.Save(strm)
strm.Position = 0
Viewer1.Document.Load(strm)
End Sub
//C#
private void Form1_Load(object sender, System.EventArgs e)
{
System.IO.MemoryStream strm = new System.IO.MemoryStream();
rptMemoryStream rpt = new rptMemoryStream();
rpt.Run();
rpt.Document.Save(strm);
strm.Position = 0;
viewer1.Document.Load(strm);
}
Samples | Walkthroughs
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.