How To: Report Sharp-Shooter – Connecting to a data source directly in a report template.
Very often we get questions from our customers like “Where in the report template can I set data source connection string?”, “Where can I set a command to fetch data for my report?”, “How to set fields of the database table for selected report elements to get data from?”, etc.
Today I decided to post an article to answer all these questions at once.
The matter is that Report Sharp-Shooter uses different mechanism of getting data for reports.
Report Sharp-Shooter stores reports in a special component – Report Manager. It contains report slots, which in their turn get report templates.
Report Manager has a DataSource property, and this very property sets data sources that can be used to build reports by templates contained in all report slots.
Thus, you see that report templates are tightly connected to the application and when you create a template you need to consider that data is propagated to the report from the application.
This mechanism is very convenient, when you have a lot of reports using a single data source. Then you just need to load data once and use it to generate multiple reports of different types; it significantly increases performance.
At the same time sometimes it is more convenient to have reports that are not based on application data. In other words, to set database connection string or command for fetching data directly in the report template and to get data right before the report is rendered (most similar products work in this way).
Unfortunately, Report Sharp-Shooter doesn’t provide complete mechanism of designing reports that are not based on the application data. There is no doubt that this function is very important and we are working add it to our product, but I can offer you an alternative solution of this problem right now.
Report Sharp-Shooter templates feature GenerateScript. It is executed right before report generation, that is why you can easily use it to set database connection code and get data.
Find below a sample of such Generate Script code:
try
{
DataObjects.Remove(“Persons”);
}
catch
{ }
OleDbConnection cn = new
OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\”database.mdb\”");
cn.Open();
string sqlCmd = “select id_person, surname, name from persons”;
OleDbDataAdapter adapt = new OleDbDataAdapter(sqlCmd, cn);
DataSet dataSet = new DataSet();
dataSet.Tables.Add(“Persons”);
dataSet.Tables["Persons"].Columns.Add(new DataColumn(“Id_person”, typeof(System.Int32)));
dataSet.Tables["Persons"].Columns.Add(new DataColumn(“Surname”, typeof(System.String)));
dataSet.Tables["Persons"].Columns.Add(new DataColumn(“Name”, typeof(System.String)));
try
{
adapt.Fill(dataSet.Tables["Persons"]);
}
catch ( Exception exc)
{
MessageBox.Show(exc.Message, “Report Sharp-Shooter”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
}
DataObjects.Add(“Persons”, dataSet.Tables["Persons"]);
The last line adds report data sources to a table.
But it is not simple, as it may seem from the first sight. The script is executed right before report generation; and it means that nothing is known about data structure when the template is generated.
That is why you need write the following code in order to get value from the table:
GetData(“”)
So, you need to remember field names, and you see that it is absolutely impossible if have a lot of them.
Let’s do as follows.
Let’s create a simple sample application. Add a DataSet and create the Persons table there. Add 3 columns: Id_person, Surname, Name. So you will get a table that is absolutely equal to to the Persons table structure that is got in the script.
Add reportManager to the application. Add the table you got to the reportManager.DataSource property.
In this way data structure will be known when designing a template.
Add report slot the reportManager – slot1; it will store edited document.
To make it more convenient to debug the template, add the “Design Template” button to our application.
Set the Click event handler:
private
void button1_Click(object sender,
EventArgs e)
{
slot1.DesignTemplate();
}
Run the designed application and click the “Design Template” button.
Create a report.
Copy the aforesaid code to the Generate Script.
Check the created template by generating report.
So, what we get? In fact, application propagates only data structure to the template, but the code for getting data itself is set directly in the template and is executed before it is generated. Moreover, when report is rendered we remove a data source set in the application:
DataObjects.Remove(“Persons“);
And add a new one got in the script instead:
DataObjects.Add(“Persons”, dataSet.Tables["Persons"]);
Then, the data source we added at the very beginning can be removed and it won’t affect report in any way.
In this way you will get report template independent from the application.
Let’s open it in the Report Designer and try to generate a report.
As you see, we get the same result.
Tags: .Net
January 28th, 2009 at 6:26 am
Good work! Thank you very much!
I always wanted to write in my site something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Regards, Timur Alhimenkov
January 28th, 2009 at 8:11 am
Timur,
Of course, you can take a part of the article and use it in your blog. A backlink is appreciated.
Thanks for visiting this blog.
Hope you will be a returning visitor.
Kind Regards,
Ekaterina Nebogina.
February 6th, 2009 at 5:32 am
Hi. Your site displays incorrectly in Firefox, but content excellent! Thank you for your wise words =)
February 17th, 2009 at 6:18 am
In Google Chrome is even better than IE. Kate can I translate to our fellows from Latin America and give of course the original link too?
[]s
Rick
February 24th, 2009 at 2:20 pm
Hmm, very cognitive post.
Is this theme good unough for the Digg?
February 26th, 2009 at 8:28 am
Angellaa,
It’s up to you to decide whether to post this article to Digg.
I think it’s a good content for developers.
Cheers,
Ekaterina.
May 13th, 2009 at 12:16 am
ebay sniper…
Interesting article. I stumbled across your site while searching for a similar topic on google. I thank you for taking the time to write about this topic….
June 5th, 2009 at 2:19 am
Hi, good post. I have been woondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.
June 16th, 2009 at 8:23 am
Hello, can you please post some more information on this topic? I would like to read more.
July 9th, 2009 at 9:12 am
Thank you for your interest in the Perpetuum Technical Blog.
Hope, our articles will be helpful to you. Later we certainly plan to write more on this topic, but unfortunately, we can’t tell you exactly, when we continue working on this problem.
Kind regards,
Ekaterina.
September 8th, 2009 at 2:02 pm
Nice job!
September 13th, 2009 at 8:36 am
Keep up the good work.
December 19th, 2009 at 8:33 am
Hello Everybody,
It is very nice to come here. It is a great forum
ass fuck