SharpShooter Reports: Passing the ConnectionString parameter programmatically.

Alexander Bub

Very often our customers are concerned with the question of passing the ConnectionString parameters to a report programmatically from within the application. We hope this article can answer the question under discussion and make the work with our reporting component much more convenient.
 
Here are the basic steps a report maker should perform:
 
1) Create an empty .rst template file
 
Create empty .rst file
 

2) Add a data source to the report manually with the help of Document Data Source Editor
 
Adding data source manually
 
And fill in the required data to create the initial connection string
 
Create initial connection string
 
3) The next step is to create a report parameter that would handle the ConnectionString.
 
Create report parameter
 
4) After this we should open the GenerateScript property of the Document to write down the script that would handle our data source and match it against the ConnectionString parameter.
 
The following code was used in our sample:
 

PerpetuumSoft.Reporting.Data.SqlBaseDataSource ds = Document.DataSources["SampleDataSource"] as PerpetuumSoft.Reporting.Data.SqlBaseDataSource; ds.ConnectionString = ((System.String) GetParameter("ConnectionString"));

 
Writing the script
 
5) After this, we set the path to our report template inside the application
 
Setting path to report template
 
6) And finally, we are able to modify the connection string parameter we created previously in our report by specifying the required data inside the application with the help of the following code:
 

reportSource.Parameters["ConnectionString"]= @"Data Source=toxocara.dyndns.org;AttachDbFilename=;Initial Catalog=SampleInventoryDB;User Id=sa; Password=342jfhdHJk89;";

 
In order to avoid any issues you should check to make sure the code is put before the lines initiating document rendering.
 
Modifying the connection string
 
The sample WPF application used in this article is attached under the link below.
 
WpfApplication12.zip
 

January 14th, 2014

Leave a Comment