Search results
Suggest a FeaturePDF

Is it possible to use a data table in Bold Reports

Yes, it is possible to use data table in Bold Reports. The data source can be added as a data table using BoldReports.Web.ReportDataSource. The steps involved in adding a sample data source using data table are provided as follows.

  • Create a SQL connection and add the data source to the report in the OnReportLoaded function.

     public void OnReportLoaded(ReportViewerOptions reportOption)
          {
             System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(@"Data Source=<instancename>;Initial Catalog=<database>;User id=<username>;Password=<password>;");
             using (connection)
             {
               reportOption.ReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource()
               {
                 ...
               });
             }
          }
  • While adding the data source, the dataset name used in the report must be provided along with the data table as shown in the following code sample.

    public void OnReportLoaded(ReportViewerOptions reportOption)
    {
      System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(@"Data Source=<instancename>;Initial Catalog=<database>;User id=<username>;Password=<password>;");
      using (connection)
      {
        reportOption.ReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource()
        {
          Name = "DataSet1",
          Value = this.GetDataTable(connection)
        });
      }
    }
    
    public System.Data.DataTable GetDataTable(System.Data.SqlClient.SqlConnection connection)
    {
      System.Data.DataSet dataset = new System.Data.DataSet();
    
      System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
    
      adapter.SelectCommand = new System.Data.SqlClient.SqlCommand(
           @"SELECT top 10 [HumanResources].[Department].[DepartmentID],[HumanResources].[Department].[Name], [HumanResources].[Department].[GroupName],[HumanResources].[Department].[ModifiedDate] FROM[HumanResources].[Department]",
           connection);
      adapter.Fill(dataset);
      return dataset.Tables[0];
    }

Here the Name is case-sensitive and it should be same as in the dataset name in the report definition. The Value also accepts IList and DataSet inputs.