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:
- On the detail section band, click the Data Source Icon.
- Click the gray area around the design surface and in the commands section at the bottom of the Properties Window, click the Edit Data Source command.
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 7 Section Report template in a Visual Studio project. See Adding an ActiveReport to a Project for information on adding different report layouts.
- In the Report Data Source dialog, on the OLE DB tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button to move to the Connection tab.
- Click the ellipsis (...) button to browse to your database, for example the NWind.mdb sample database. Click Open once you have selected the appropriate database path.
- Click the Test Connection button to see if you have successfully connected to the database.
- Click OK to close the Data Link Properties window and return to the Report Data Source dialog. Notice that the Connection String field gets filled automatically.
- In the Query field on the OLE DB tab, enter a SQL query to select the data that you want use from the connected database. For example,
Select * From CUSTOMERS
- Click OK to save the data source and return to the report design surface.
- In the Report Data Source dialog, on the SQL tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft OLE DB Provider for SQL Server and click the Next button to move to the Connection tab.
- On the Connection tab of the Data Link Properties window:
- In the Select or enter server name field, select your server from the drop down list.
- Under Enter information to log on to the server, select the Windows NT security credentials or your specific user name and password.
- Under Select the database on the server, select a database from the server or attach a database file.
- Click the Test Connection button to see if you have successfully connected to the database.
- Click OK to close the Data Link Properties window and to return to the Report Data Source dialog. Notice that the Connection String field gets filled automatically.
- In the Query field on the SQL tab, enter a SQL query to select the data that you want use from the connected database. For example,
Select * From CUSTOMERS
- Click OK to save the data source and return to the report design surface.
- In the Report Data Source dialog, on the XML tab, click the ellipsis (...) button next to File URL field.
- In the Open File window that appears, navigate to your XML data file to select it and click the Open button. You can use a sample XML data file located at C:\Users\YourUserName\Documents\ComponentOne Samples\ActiveReports Developer 7\Data\customer.xml.
- In the Recordset Pattern field, enter a valid XPath expression like the following.
//CUSTOMER
- Click OK to save the data source and return to the report design surface.
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.
- Add an Imports (VisualBasic.NET) or using (C#) statement for System.Data and System.Data.Oledb namespaces.
- Right-click the gray area outside the design surface to select the report and select Properties.
- In the Properties window that appears, click the Events icon to view the available events for the report.
- In the events list, double-click the ReportStart event. This creates an event-handling method for the ReportStart event in code.
- Add the following code to the handler.
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 Developer 7\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()
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 Developer 7\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();
- Right-click the gray area outside the design surface to select the report and select Properties.
- In the Properties window that appears, click the Events icon to view the available events for the report.
- In the events list, double-click the ReportEnd event. This creates an event-handling method for the ReportEnd event.
- Add the following code to the handler.
To write the code in Visual Basic
Visual Basic.NET code. Paste inside the ReportEnd event. Copy Code m_reader.Close() m_cnn.Close()
C# code. Paste inside the ReportEnd event. Copy Code m_reader.Close(); m_cnn.Close();
- Right-click the gray area around the design surface to select the report and select Properties.
- In the Properties window that appears, click the Events icon to view the available events for the report.
- In the events list, double-click DataInitialize event. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's 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")
C# code. Paste inside the DataInitialize event. Copy Code Fields.Add("CategoryName"); Fields.Add("ProductName"); Fields.Add("UnitsInStock"); Fields.Add("Description");
- Right-click the gray area around the design surface to select the report and select Properties.
- In the Properties window that appears, click the Events icon to view the available events for the report.
- In the events list, double-click the FetchData event. This creates an event-handling method for the report's FetchData event.
- Add the following code to the handler to retrieve information to populate the report 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
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
- Right-click the design surface and select View Code.
- Add the following code inside the class declaration of the report:
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
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()); } }
- On the design surface, right-click the gray area around the design surface to select the report and select Properties.
- In the Properties window that appears, click the Events icon to view the available events for the report.
- Double-click the DataInitialize event. This creates an event-handling method for the report's DataInitialize event.
- Add the following code to the handler to add fields to the report's Fields collection.
Visual Basic.NET code. Paste inside the DataInitialize event. Copy Code Me.Fields.Add("TestField") Me.list = New List(Of String) datasource1 = GetIEnumerableData().GetEnumerator()
C# code. Paste inside the DataInitialize event. Copy Code this.Fields.Add("TestField"); datasource = GetIEnumerableData().GetEnumerator();
- Repeat steps 3 and 4 to open the events list in the property window.
- Double-click the FetchData event. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
To populate fields in Visual Basic
Visual Basic.NET code. Paste inside the FetchData event. Copy Code If datasource1.MoveNext() Then
Me.Fields("TestField").Value = datasource1.Current
eArgs.EOF = False
Else
eArgs.EOF = True
End IfC# 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. |