473,520 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How-to store FastReport.NET report templates in database

HERBERTS
16 New Member
Some applications require the storing of report templates in a database. This simplifies template support - all reports are stored in one place, and allows to differentiate the access rights to different templates. I noted only one shortcoming of this technique - slight complication of the code in the client application.
For example, we need to create Access database (mdb) named "reports" and a table for storing our report templates. I named this table "reports_table". Three tables are enough in our data table:
Expand|Select|Wrap|Line Numbers
  1. №    Field name    Type    Description
  2. 1    Id            counter    Auto-increment, index
  3. 2    ReportName    text    Name of report
  4. 3    ReportFile    BLOB    Binary data with template
You can store templates in any other database similar to the one in our example.
To do this:
  1. open the designer;
  2. show custom form with a list of templates from our database instead of standard dialog when users open a file in the designer;
  3. load template in the designer after selecting it in the custom form;
  4. Save the template in a database where the user saves a file in the designer.
Despite the apparent complexity, the tasks above will be simple because FastReport.NET has special features.
Create a new project in Visual Studio first. Then you need to drag-n-drop "Report" object from toolbox to the empty form. Also drag the " EnvironmentSettings" object (properties of reporting engine).

You can setup many settings of the report and the designer in "environmentSettings1" object. You have to override the event handlers CustomOpenDialog, CustomOpenReport, CustomSaveDialog and CustomSaveReport.
Event handler CustomOpenDialog allows releasing any algorithm of the load report template in the designer - load template from the database field and load it in the designer or set e.Cancel = true; when loading fails.
The problem reduces to showing a dialogue with a list of files from which you want to select the report template from the database and load the template in the designer. Of course, if the user presses the Cancel button in this dialog, we set e.Cancel = true. The loading process is clear but saving code need some details.
The user can choose two ways to save a file - simply by clicking "Save", in this case report should be rewritten to the old place, or the second option - "Save as...”, the user is expected to select path of file. Storage location of report templates cannot be changed in our case. You need to save template in the old place (database field) in both ways - capture the CustomSaveDialog event by empty function and save template in database in the CustomSaveReport event.
Now you need to add a new data source to the project - reports.mdb.Drop the button on form for launching of the reports designer.

Write in the Click event handler:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.   report1.Design();
  4. }
Create an additional dialog form for template selection:

Assign the buttons OK and Cancel corresponding to DialogResult.
Do not forget to bind your data source to DataGrid - select the field name of the record and make the index field invisible.
You need to save the selected index when you click on the ‘OK’ button:
Expand|Select|Wrap|Line Numbers
  1. public int reportID;
  2. //...
  3. private void OKBtn_Click(object sender, EventArgs e)
  4. {
  5.   // save id of report for use in future
  6.     reportID = (int)dataGridView1.CurrentRow.Cells[0].Value;
  7. }
We return to our first form. It is time to handle the events of opening and saving a report. We make loading the first thing to do:
Expand|Select|Wrap|Line Numbers
  1. // define variable for store of report ID
  2. private int reportID;
  3. // define array byte[] for store of template
  4. private byte[] blob;
  5.  
  6. //....
  7. private void environmentSettings1_CustomOpenDialog(object sender,   
  8.      FastReport.Design.OpenSaveDialogEventArgs e)
  9. {
  10.   using (ReportListForm reportListForm = new ReportListForm())
  11.   {
  12.     // show dialog for report selection
  13.     if (reportListForm.ShowDialog() == DialogResult.OK)
  14.     {
  15.       // get report ID 
  16.       reportID = reportListForm.reportID;
  17.       // load report in array from BLOB
  18.       blob = 
  19.          (byte[])this.reports_tableTableAdapter.GetDataByID(reportID).Rows[0]["ReportFile"];
  20.       // read file name of report for designers title
  21.       e.FileName =  
  22.          (string)this.reports_tableTableAdapter.GetDataByID(reportID).Rows[0]["ReportName"];
  23.     }
  24.     else
  25.       // cancel loading
  26.       e.Cancel = true;
  27.   }            
  28. }
Second handler CustomOpenReport for loading the template in designer should look like this:
Expand|Select|Wrap|Line Numbers
  1. private void environmentSettings1_CustomOpenReport(object sender, 
  2.   FastReport.Design.OpenSaveReportEventArgs e)
  3. {
  4.   using (MemoryStream stream = new MemoryStream())
  5.   {
  6.     // skip all garbage created by MS Access in begin of blob field - we seek the tag of XML
  7.     int start = 0;
  8.     for (int i = 0; i < blob.Length - 1; i++)
  9.     {
  10.       if (blob[i] == (byte)'<' && blob[i + 1] == (byte)'?')
  11.       {
  12.         start = i;
  13.         break;
  14.       }
  15.     }
  16.     // copy of blob content in stream
  17.     stream.Write(blob, start, blob.Length - start);
  18.     stream.Position = 0;
  19.     // load template in designer    
  20.     e.Report.Load(stream);
  21.   }
  22. }
We used mdb-database to store report templates. MS Access write special header in begin of BLOB field. We need to skip this header and find begin of xml file (report templates stored in xml).
We turn to the organization and save the template using the designer. As written above, we need to override the event handler CustomSaveDialog by an empty function to prevent a user from unnecessary actions:
Expand|Select|Wrap|Line Numbers
  1. private void environmentSettings1_CustomSaveDialog(object sender,
  2.    FastReport.Design.OpenSaveDialogEventArgs e)
  3. {
  4.   // empty
  5. }
Event handler CustomSaveReport should look like this:
Expand|Select|Wrap|Line Numbers
  1. private void environmentSettings1_CustomSaveReport(object sender, 
  2.   FastReport.Design.OpenSaveReportEventArgs e)
  3. {
  4.   // create stream for store of template
  5.   using (MemoryStream stream = new MemoryStream())
  6.   {
  7.     // save template in stream
  8.     e.Report.Save(stream);
  9.     // copy stream in array 
  10.     byte[] blob = stream.ToArray();
  11.     // save array in BLOB                               
  12.     this.reports_tableTableAdapter.UpdateReport(blob, reportID);                
  13.   }            
  14. }
The methods GetDataByID and UpdateReport are created in the dataset designer- you can see them in the example project.
Compile and run application, then press a button to open the designer:
  1. Click menu item "Open":
  2. Select any report template and press OK.

You can change the report and save the template after that- new template will be stored in database.
Full source code of this example is available in an attached zip archive (databases with reports are included).
Jul 23 '10 #1
3 15324
tottiacmilan17
2 New Member
It is very good article ,
but I have problem with implementation ,
if possible plz provide me a Full source code + DB because it is not attached to this article ,
I have done all your code in my app but the problem is loading section that return error for me "the template format is not appropriate"
Apr 29 '14 #2
tottiacmilan17
2 New Member
I figure out the problem and find my own answer , and shared the answer in following link
http://stackoverflow.com/questions/2...72488#23372488
Apr 29 '14 #3
Gerzegeh
1 New Member
I cant doewnload zip file of How-to store FastReport.NET report templates in database
Jul 17 '22 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4698
by: BKDotCom | last post by:
Perhaps not PHP specific, but: Both http://www.bankofamerica.com/ and http://www.bankone.com/ have account login forms on their non-secure main pages. How on earth are they accomplishing this? Please enlighten me.
21
9753
by: Cpt. Zeep | last post by:
If someone can help me; how much EUR per hour (average) is standard payment for PHP programmers in Germany? Thank you! -- Relaxen und watch das blinkenlights...
3
7327
by: | last post by:
Well, I read RFC, but I have a problem now: How can I get the HTTP_RANGE enviroment variable from client to resume a download? (GetRight) I try $http_range = $_ENV; $http_range = $_COOKIE; but no result ;-( Please Help!
4
3248
by: berehneh | last post by:
i have a site in a host with adomain i can run php with .php files but i can 't use php in .htm file. how can i do this
3
1959
by: Joe Cybernet | last post by:
people keep interrupting my script by pressing their Stop buttons. how can I prevent?
1
2422
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to...
9
3929
by: Timothy Madden | last post by:
Hy all In my application I'll receive XML text in the HTTP-message body in a particular format that only my application understands My script will be requested by the client with HTTP POST and the HTTP request will contain the XML as reqest-message body I couldn't find a way to acces the POST message body in my script to parse the XML...
5
1999
by: R. Rajesh Jeba Anbiah | last post by:
Anyone knows how ASP detects frame? I couldn't find any PHP solutions yet. TIA -- | Just another PHP saint | Email: rrjanbiah-at-Y!com
2
1804
by: kindermaxiz | last post by:
i have a variable $var1 if $var1 = "xyz" how can I create a variable called $xyz because $xyz is defined in a included file. how can I do that? thanx in advance
7
2557
by: anushhprabu | last post by:
#define q(k)main(){ return!puts(#k"\nPRABUq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) guys i'm working on this code.. i got it fromnet.. how this is working.. anyone pls.. it is printing the same content on screen.. how ya.. how it is.. how how how?? please...................
0
7222
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7615
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5758
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5141
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3287
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.