ActiveReports 16 .NET Edition
ActiveReports 16 User Guide / How To / Section Report How To / Report Data / Bind Reports to a Data Source
In This Topic
    Bind Reports to a Data Source
    In This Topic

    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.

    Report DataSource dialog

    Report DataSource dialog

    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 16 Section Report template in a Visual Studio project. See Basic Concepts further information on adding different report layouts.

    To use an Unbound data source

    To create a data connection

    1. Add an Imports (VisualBasic.NET) or using (C#) statement for System.Data and System.Data.Oledb namespaces.
    2. Right-click the gray area outside the design surface to select the report and select Properties.
    3. In the Properties window that appears, click the Events icon to view the available events for the report.
    4. In the events list, double-click the ReportStart event. This creates an event-handling method for the ReportStart event in code.
    5. 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=[User Folder]\Samples16\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="
              + @"=[User Folder]\Samples16\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

    1. Right-click the gray area outside the design surface to select the report and select Properties.
    2. In the Properties window that appears, click the Events icon to view the available events for the report.
    3. In the events list, double-click the ReportEnd event. This creates an event-handling method for the ReportEnd event.
    4. 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()
      

      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

    1. Right-click the gray area around the design surface to select the report and select Properties.
    2. In the Properties window that appears, click the Events icon to view the available events for the report.
    3. In the events list, double-click DataInitialize event. This creates an event-handling method for the report's DataInitialize event.
    4. 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")
      

      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

    1. Right-click the gray area around the design surface to select the report and select Properties.
    2. In the Properties window that appears, click the Events icon to view the available events for the report.
    3. In the events list, double-click the FetchData event. This creates an event-handling method for the report's FetchData event.
    4. 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
      

      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 run time, 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

    1. Right-click the design surface and select View Code.
    2. 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
      

      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());
          }
      }
      
    3. On the design surface, right-click the gray area around the design surface to select the report and select Properties.
    4. In the Properties window that appears, click the Events icon to view the available events for the report.
    5. Double-click the DataInitialize event. This creates an event-handling method for the report's DataInitialize event.
    6. Add the following code to the handler to add fields to the report's Fields collection.

      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();
      
    7. Repeat steps 3 and 4 to open the events list in the property window.
    8. Double-click the FetchData event. This creates an event-handling method for the report's FetchData event.
    9. 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 If

      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 run time, 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.
    See Also