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.
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 on Build...
- 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
- Add a GroupHeader/Footer section to rptMemoryStream.
-
Make the following changes to the group header:
- Change the name to ghCustomers
- Change the DataField property to Country
-
Add the following controls to the rptMemoryStream:
| Control |
DataField |
Name |
Text/Caption |
Section |
Location |
|
TextBox |
Country |
txtCountry |
Country |
GroupHeader |
0,
0 |
|
TextBox |
CustomerID |
txtCustomerID |
Customer ID |
Detail |
0,
0 |
| TextBox |
CompanyName |
txtCompanyName |
Company Name |
Detail |
1.125, 0 |
| TextBox |
Address |
txtAddress |
Address |
Detail |
3.01, 0 |
| TextBox |
City |
txtCity |
City |
Detail |
4.375, 0 |
| TextBox |
Phone |
txtPhone |
Phone |
Detail |
5.437, 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 click on 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
(Base Class 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 on 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)
Dim theBytes(strm.Length) As Byte
strm.Read(theBytes, 0, Int(strm.Length))
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);
byte[] theBytes = new byte[strm.Length];
strm.Read(theBytes, 0, (int)strm.Length);
strm.Position =0;
viewer1.Document.Load(strm);
}
Samples | Walkthroughs
Copyright © 2005 Data Dynamics, Ltd. All rights reserved.