[]
Using a database as a data source is one of the most common scenarios when you work with reports. This topic explains how to bind your ActiveReports report to a SQL, OLEDB or ODBC data source.
The example below uses a SQL database as a data source.
private PageReport LoadReport()
{
string path = ""; // Your path to the report
PageReport report = new PageReport(new FileInfo(path));
report.Document.LocateCredentials += OnLocateCredentials;
string dataProviderName = "SQL"; // Or use "OLEDB" or "ODBC" if you need it
DataSource dataSource = report.Report.DataSources[0]; // Your data source
if(dataSource.ConnectionProperties.DataProvider == dataProviderName)
{
dataSource.ConnectionProperties.ConnectString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
}
return report;
}
private void OnLocateCredentials(object sender, LocateCredentialsEventArgs args)
{
args.Password = ""; // Your password if you need it
args.UserName = ""; // Your username if you need it
}
Use the following strings to connect to different data providers:
SQL - Use this if you connect using the MS SQL Server. An example of a connection string:
Data Source=[sourceName];Initial Catalog=[dbName];Integrated Security=True;Connect Timeout=30;Encrypt=False;
OLEDB - Use this if you connect using the OLEDB driver. An example of a connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[dbname];
ODBC - se this if you connect using the ODBC driver. An example of a connection string:
DRIVER=SQLite3 ODBC Driver;Database=[dbname]; LongNames=0; Timeout=1000; NoTXN=0; SyncPragma=NORMAL; StepAPI=0;
type=note
Note: When you are using an ODBC/OLEDB data provider, make sure that the required driver is installed on your computer. Otherwise, you cannot use this data provider.
When working with a data provider, you may need to process user credentials to work with the target source. The LocateCredentials event is used for this purpose.
report.Document.LocateCredentials += OnLocateCredentials;
Assign a handler that specifies the password and username when they are requested.
private void OnLocateCredentials(object sender, LocateCredentialsEventArgs args)
{
args.Password = ""; // Your password if you need it
args.UserName = ""; // Your username if you need it
}
The event handler receives an argument of the LocateCredentialsEventArgs type that contains data, related to this event. The following LocateCredentialsEventArgs properties provide information, specific to this event.
type=note
Note: We assume that the report has defined data fields and datasets, which can be done in various ways, e.g., in the Web or Windows Forms Designer.