ActiveReports 9 > ActiveReports User Guide > How To > Section Report How To > Work with Data in Section Reports > Bind Reports to a Data Source |
At design time, you can connect a section report to a data source through the Report Data Source dialog. You can access the Report Data Source dialog by doing one of the following:
There are four tabs in the dialog for the four most commonly used data sources.
The following steps take you through the process of binding reports to each data source. These steps assume that you have already added an ActiveReports 9 Section Report template in a Visual Studio project. See Adding an ActiveReport to a Project further information on adding different report layouts.
To use the OLE DB data source
Select * From CUSTOMERS
To use the SQL data source
Select * From CUSTOMERS
To use the XML data source
//CUSTOMER
You also have the option to use an unbound or an IEnumerable data source. See the following procedures to implement these data source connections in code.
To use an Unbound data source
To create a data connection
To write code in VisualBasic.NET
Visual Basic.NET code. Paste above the ReportStart event. |
Copy Code
|
---|---|
Dim m_cnnString As String Dim sqlString As String Dim m_reader As OleDbDataReader Dim m_cnn As OleDbConnection |
Visual Basic.NET code. Paste inside the ReportStart event. |
Copy Code
|
---|---|
'Set data source connection string.
m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
+ "Data Source=C:\Users\YourUserName\Documents\ComponentOne Samples\ActiveReports 9\Data\Nwind.mdb;Persist Security Info=False"
'Set data source SQL query.
sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid " _
+ "= products.categoryid ORDER BY products.categoryid, products.productid"
'Open connection and create DataReader.
m_cnn = New OleDb.OleDbConnection(m_cnnString)
Dim m_Cmd As New OleDb.OleDbCommand(sqlString, m_cnn)
If m_cnn.State = ConnectionState.Closed Then
m_cnn.Open()
End If
m_reader = m_Cmd.ExecuteReader()
|
To write code in C#
C# code. Paste above the ReportStart event. |
Copy Code
|
---|---|
private static OleDbConnection m_cnn;
private static OleDbDataReader m_reader;
private string sqlString;
private string m_cnnString;
|
C# code. Paste inside the ReportStart event. |
Copy Code
|
---|---|
//Set data source connection string.
m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ @"C:\Users\YourUserName\Documents\ComponentOne Samples\ActiveReports 9\Data\Nwind.mdb;Persist Security Info=False";
//Set data source SQL query.
sqlString = "SELECT * FROM categories INNER JOIN products"
+ " ON categories.categoryid = products.categoryid"
+ " ORDER BY products.categoryid, products.productid";
//Open connection and create DataReader.
m_cnn = new OleDbConnection(m_cnnString);
OleDbCommand m_Cmd = new OleDbCommand(sqlString,m_cnn);
if(m_cnn.State == ConnectionState.Closed)
{
m_cnn.Open();
}
m_reader = m_Cmd.ExecuteReader();
|
To close the data connection
To write the code in Visual Basic
Visual Basic.NET code. Paste inside the ReportEnd event. |
Copy Code
|
---|---|
m_reader.Close() m_cnn.Close() |
To write the code in C#
C# code. Paste inside the ReportEnd event. |
Copy Code
|
---|---|
m_reader.Close(); m_cnn.Close(); |
To create a fields collection
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste inside the DataInitialize event. |
Copy Code
|
---|---|
Fields.Add("CategoryName") Fields.Add("ProductName") Fields.Add("UnitsInStock") Fields.Add("Description") |
To write the code in C#
C# code. Paste inside the DataInitialize event. |
Copy Code
|
---|---|
Fields.Add("CategoryName"); Fields.Add("ProductName"); Fields.Add("UnitsInStock"); Fields.Add("Description"); |
To populate the fields
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste inside the FetchData event. |
Copy Code
|
---|---|
Try m_reader.Read() Me.Fields("CategoryName").Value = m_reader("CategoryName") Me.Fields("ProductName").Value = m_reader("ProductName") Me.Fields("UnitsInStock").Value = m_reader("UnitsInStock") Me.Fields("Description").Value = m_reader("Description") eArgs.EOF = False Catch ex As Exception eArgs.EOF = True End Try |
To write the code in C#
C# code. Paste inside the FetchData event. |
Copy Code
|
---|---|
try { m_reader.Read(); Fields["CategoryName"].Value = m_reader["CategoryName"].ToString(); Fields["ProductName"].Value = m_reader["ProductName"].ToString(); Fields["UnitsInStock"].Value = m_reader["UnitsInStock"].ToString(); Fields["Description"].Value = m_reader["Description"].ToString(); eArgs.EOF = false; } catch { eArgs.EOF = true; } |
Tip: In order to view the added data at runtime, add controls to your report and assign their DataField property to the name of the fields you added in code while creating a field collection. |
Caution: Do not access the Fields collection outside the DataInitialize and FetchData events. Accessing the Fields collection outside of these events is not supported, and has unpredictable results. |
To use the IEnumerable data source
To create a data source in Visual Basic
Visual Basic.NET code. Paste inside the class declaration of the report. |
Copy Code
|
---|---|
Private datasource1 As IEnumerator(Of String) = Nothing Dim list As List(Of String)= Nothing |
Visual Basic.NET code. Paste inside the class declaration of the report. |
Copy Code
|
---|---|
Private Function GetIEnumerableData() As IEnumerable(Of String) For i As Integer = 1 To 10 list.Add(String.Format("TestData_{0}", i.ToString())) Next Return list End Function |
To create a data source in C#
C# code. Paste inside the class declaration of the report. |
Copy Code
|
---|---|
private IEnumerator<string> datasource = null; |
C# code. Paste inside the class declaration of the report. |
Copy Code
|
---|---|
private IEnumerable<string> GetIEnumerableData() { for (int i = 1; i <= 10; i++) { yield return string.Format("TestData_{0}", i.ToString()); } } |
To add fields in Visual Basic
Visual Basic.NET code. Paste inside the DataInitialize event. |
Copy Code
|
---|---|
Me.Fields.Add("TestField") Me.list = New List(Of String) datasource1 = GetIEnumerableData().GetEnumerator() |
To add fields in C#
C# code. Paste inside the DataInitialize event. |
Copy Code
|
---|---|
this.Fields.Add("TestField"); datasource = GetIEnumerableData().GetEnumerator(); |
To populate fields in Visual Basic
Visual Basic.NET code. Paste inside the FetchData event. |
Copy Code
|
---|---|
If datasource1.MoveNext() Then |
To populate fields in C#
C# code. Paste inside the FetchData event. |
Copy Code
|
---|---|
if (datasource.MoveNext()) { this.Fields["TestField"].Value = datasource.Current; eArgs.EOF = false; } else eArgs.EOF = true; |
Tip: In order to view the added data at runtime, add controls to your report and assign their DataField property to the name of the fields you added in code while creating a field collection. |