Glossary Item Box
ActiveReports allows you to change the data source of a report at run time based on the location of the sample database file on the user's computer.
This walkthrough illustrates how to set up a report to set a report's data source at run time.
This walkthrough is split up into the following activities:
To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb).
To add an ActiveReport to your project
To connect the data source to a database
To add controls to the report
Control | DataField | Name | Text/Caption | Location |
OutputFormat |
---|---|---|---|---|---|
TextBox | ProductID | txtProductID | Product ID | 0, 0.625 | (Empty string) |
TextBox | ProductName | txtProductName | Product Name | 1.125, 0.0625 | (Empty string) |
TextBox | UnitsInStock | txtUnitsInStock | Units In Stock | 2.25, 0.0625 | (Empty string) |
TextBox | UnitsOnOrder | txtUnitsOnOrder | Units On Order | 3.375, 0.0625 | (Empty string) |
TextBox | UnitPrice | txtUnitPrice | Unit Price | 4.5, 0.0625 | Currency |
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the function looks like.
' Visual Basic
Private Function getDatabasePath() As String
Dim regKey As RegistryKey
regKey = Registry.LocalMachine
regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET _ 2.0\\SampleDB")
getDatabasePath = CType(regKey.GetValue(""), String)
End Function
//C#
private string getDatabasePath()
{
RegistryKey regKey = Registry.LocalMachine;
regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 2.0\\SampleDB");
return (string)regKey.GetValue("");
}
To write the code in Visual Basic
To write the code in C#
The following example shows what the code for the method looks like.
' Visual Basic
Dim m_cnn As OleDbConnection
Dim m_dbpath As String
Dim m_cnnString As String
Dim sqlString As String
Private Sub rptModifyDS_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.ReportStart
m_dbpath = getDatabasePath()
m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_dbpath + "\\NWind.MDB"
sqlString = "SELECT * from products" m_cnn = New OleDbConnection(m_cnnString)
If m_cnn.State = ConnectionState.Closed Then
m_cnn.Open()
End If
m_reader = m_Cmd.ExecuteReader()
Me.DataSource = m_reader
End Sub
//C#
private static OleDbConnection m_cnn;
private static OleDbDataReader m_reader;
private void rptModifyDS_ReportStart(object sender, System.EventArgs eArgs)
{
string m_dbPath = getDatabasePath();
string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_dbPath + "\\NWind.MDB";
string sqlString = "SELECT * from products";
m_cnn = new OleDbConnection(m_cnnString);
if(m_cnn.State == ConnectionState.Closed)
{
m_cnn.Open();
}
m_reader = m_Cmd.ExecuteReader();
this.DataSource = m_reader;
}
To view the report
See Also |
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.