How to Change Connection String of Query in Infragistics Report at Run Time
Friends,
In a previous article, we saw how we can create a report using Infragistics Reporting. If you have not read it so far, you can read it here. In that post you might have noticed that we are using the in-built SQL Data Source from the “Report Data Explorer” window. Now, normally we develop on our own development environment and when the code is deployed on a production environment, the database is completely different. So, we need to make sure that the report reads the query string from some configuration file (app.config/web.config) or some other common place in the project that can be changed easily. In this post, we will how we can “Change Connection String in Infragistics Report at Run Time”. 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” which implements “IReportDbConnectionProvider” interface. We add a class to our project as show below and name it as “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 below –
- Implement the following code in your “ReportDbConnectionProvider.cs” file.
public DbConnection GetConnection(string name, IDictionary reportParameters) { switch (name) { case "sqlDatSource1": return new SqlConnection(@"Data Source=NITESH\SQLEXPRESS;Initial Catalog=SampleDB;Integrated Security=True"); default: return null; } }
- 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” which is the Data Source used by our project.
- Now, when you execute the project, your report will use the new connection string.
Hope you like this article. Keep learning and sharing!