How to Change the ConnectionString of a Query in Infragistics Report

Friends, in a previous article, we saw how to create a report using Infragistics Reporting. If you have not read it so far, you can read it here. In that article you might have noticed that we are using the built-in SQL Data Source from the “Report Data Explorer” window. Now, normally we develop in our own development environment and when the code is deployed on a production environment, the database is completely different. So, we need to ensure that the report reads the query string from app.config or some other common place in the project that can be changed easily. In this article, we will see how to do that. Let's get started.

We open the same Visual Studio project that we created in our article here.

Infragistics provides a specific template named “Infragistics Report Database Connection Provider” that implements the “IReportDbConnectionProvider” interface. We add a class to our project as shown below and name it “ReportDbConnectionProvider.cs”.

By default the template gives you the following code.

Now we need to identify the name of the Data Source used in our report. This can be found under the Data Sources section in the “Report Data Explorer” window as in the following.

Implement the following code in your “ReportDbConnectionProvider.cs” file.

  1. public DbConnection GetConnection(string name, IDictionary < string, ParameterValue > reportParameters)  
  2. {  
  3.     switch (name)   
  4.     {  
  5.         case "sqlDatSource1":  
  6.             return new SqlConnection(@  
  7.             "Data Source=NITESH\SQLEXPRESS;Initial Catalog=SampleDB;Integrated Security=True");  
  8.         default:  
  9.             return null;  
  10.     }  
  11. }  

As you see above, I have added code in GetConnection() and returned an SqlConnection object with my custom connection string for the data source named “sqlDataSource1” that is the Data Source used by our project.

Now, when you execute the project, your report will use the new connection string.

I hope you like this article. Keep learning and sharing.