Loading FastReport from DataTable

Hi everyone, I'm new with Fast Reports and so far I have found it to be very easy to use. I just ran into an issue and can't find a solution anywhere.

I have made a report in the designer using a SQL Database Connection. This works well, but I actually need to manupulate some of the data in my application first.

My intention is to us the same SQL statement to get the data, edit some fields and then I would like to send the DataTable to the report to load rather than have it pull directly from the SQL Database.

How would I do this? I've found where I can pass the connection string and sql statement, but I haven't found any other options for passing anything else to the report or for loading the report from a DataTable.

Also, I would like to load an image in the header. I was able to put the image in through the designer, but I would like to do this in code because the image could change. How would I do this?

Thanks Again for your help.
-Matt-

Comments

  • edited January 2017
                    using (SqlConnection cnn = new SqlConnection(yourSQLConnectionString))
                    {
                        cnn.Open();
    
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.Connection = cnn;
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = "SELECT .. FROM ... WHERE ... = @..";
                            cmd.Parameters.AddWithValue("@...", ...);
                            using (SqlDataReader dr = cmd.ExecuteReader())
                            {
                                using (DataTable dt = new DataTable())
                                {
                                    dt.Load(dr);
                                    dt.TableName = "MainReport";
    
                                        //ADDING COLUMN TO DATATABLE
                                        dt.Columns.Add("NewColumn", typeof(string));
                                        //if your datable contains many many rows, use parallel.foreach for faster looping
                                        foreach (DataRow row in dt.Rows)
                                        {
                                            row[12] =  NumberToEnglishWords.SpellNumber((decimal)(row[8]));
                                        }
    
                                    string fullpathname = @"d:\myLogo.png";
                                    FastReport.Report report = new  FastReport.Report();
                                    report.load(yourfrxfile);
                                    report.Preview = PreviewControl1;
                                    //pass value as report parameter
                                    report.SetParameterValue("FullPathNameOfPictureFile", fullpathname);
                                    report.RegisterData(dt, dt.TableName);
                                    report.Prepare();
                                    report.ShowPrepared();
                                }
                            }
                        }
                    }
    

    and in frx file, add script (startreport event)
    namespace FastReport
    {
      public class ReportScript
      {
        private void _StartReport(object sender, EventArgs e)
        {
          Picture1.ImageLocation = (String)Report.GetParameterValue("FullPathNameOfPictureFile"); 
        }
      }
    }
    </ScriptText>
    

    note: c# is a strong type language, therefore, datasource in your frx file must match with datatable :
    <TableDataSource Name="MainReport" ReferenceName="MainReport" DataType="System.Int32" Enabled="true">
    <Column Name="0" DataType="System.Int32"/>
    <Column Name="1" DataType="System.DateTime"/>
    <Column Name="2" DataType="System.String"/>
    <Column Name="3" DataType="System.Int32"/>
    <Column Name="4" DataType="System.Int32"/>
    <Column Name="5" DataType="System.Decimal"/>
    <Column Name="6" DataType="System.Decimal"/>
    <Column Name="7" DataType="System.Decimal"/>
    <Column Name="8" DataType="System.Decimal"/>
    <Column Name="9" DataType="System.Decimal"/>
    <Column Name="10" DataType="System.Decimal"/>
    <Column Name="11" DataType="System.Decimal"/>
    <Column Name="12" DataType="System.String"/>
    </TableDataSource>
  • edited 10:24PM
    Thank you so much for your help. I am going to try this today!

    Is it alright that I connected my report to my SQL database so that I could drag and drop the fields? Or do I need to design the report differently as well?

    Thanks so much!
  • edited January 2017
    Load datasource from your .net project, then design the report.
    // IF YOU WANT TO REUSE PREVIEWCONTROL1 FOR ANOTHER REPORT, DON'T FORGET THIS CODE
    if (PreviewControl1.Report != null)
    {
        PreviewControl1.Clear();
        PreviewControl1.Report.Dispose();
    }
    
    FastReport.Report report = new  FastReport.Report();
    
    //IF YOU WANT TO DESIGN A REPORT FOR THE VERY FIRST TIME, COMMENT THE CODE BELOW
    //UNCOMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    //report.load(yourfrxfile);
    //report.Preview = PreviewControl1;
    
    report.SetParameterValue("FullPathNameOfPictureFile", fullpathname);
    report.RegisterData(dt, dt.TableName);
    
    //COMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    report.GetDataSource(dt.TableName).Enabled = true;
    report.Design(true);
    
    //IF YOU WANT TO DESIGN A REPORT FOR THE VERY FIRST TIME, COMMENT THE CODE BELOW
    //UNCOMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    //report.Prepare();
    //report.ShowPrepared();
    
  • edited 10:24PM
    Ooooh...

    Thank you so much, it never occurred to me that I would need to design the report during runtime as opposed to in the Fast-Reports Design Program. I will try that today.

    Thank you very much for all of your help! This makes a lot more sense to me now.
  • edited 10:24PM
    I'm very sorry to bother you again.

    I have been able to get everything working with the exception of the image.

    In my demo project, I have it like so:
    string fullpathname = @C:\Logo_Square.jpg;

    // IF YOU WANT TO REUSE PREVIEWCONTROL1 FOR ANOTHER REPORT, DON'T FORGET THIS CODE
    if (fastreportspreviewControl1.Report != null)
    {
    fastreportspreviewControl1.Clear();
    fastreportspreviewControl1.Report.Dispose();
    }

    FastReport.Report report = new FastReport.Report();

    //IF YOU WANT TO DESIGN A REPORT FOR THE VERY FIRST TIME, COMMENT THE CODE BELOW
    //UNCOMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    report.Load(@C:\Users\Matthew L\Desktop\Development Work\Reports\FastReports\PDInvoice.frx);
    report.Preview = fastreportspreviewControl1;

    report.SetParameterValue("FullPathNameOfPictureFile", fullpathname);

    report.RegisterData(dt, dt.TableName);

    //COMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    //report.GetDataSource(dt.TableName).Enabled = true;
    //report.Design(true);

    //IF YOU WANT TO DESIGN A REPORT FOR THE VERY FIRST TIME, COMMENT THE CODE BELOW
    //UNCOMMENT WHEN YOU FINISHED WITH REPORT DESIGNER
    report.Prepare();
    report.ShowPrepared();

    In my report, I have a picturebox named Picture1 and the following in the Code tab:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Data;
    using FastReport;
    using FastReport.Data;
    using FastReport.Dialog;
    using FastReport.Barcode;
    using FastReport.Table;
    using FastReport.Utils;

    namespace FastReport
    {
    public class ReportScript
    {
    private void StartReport(object sender, EventArgs e)
    {
    Picture1.ImageLocation = (String)Report.GetParameterValue("FullPathNameOfPictureFile");
    }
    }
    }

    Am I missing something? I am not getting an error of any kind, the picturebox just remains blank...

    I'm very sorry to keep bothering you and wanted to thank you for all of your help.
  • edited 10:24PM
    have you done this?
  • edited 10:24PM
    Thank you for your response.

    Yes, I did add that Parameter and rename it to FullPathNameOfPictureFile.

    I actually added a picturebox to my form and set it's image to the fullpathname and it does load in the pictureBox so I'm not sure why it wouldn't be loading in the report...

    I was able to, as a work around, include the image file path as a column in my datatable and set the image in the report to this value and this did work. So I was able to get the image in there, just a different way. I don't know if this would be considered a bad method for any particular reason?

    Thanks again for your help, I really appreciate it.
  • edited 10:24PM
    sounds like you forgot to attach the code to Report Start Event
  • edited 10:24PM
    I'm sorry, you're right. I don't know how I missed that...

    I have it working now, thank you very much for all of your help, I really appreciate it.

    I am really impressed by Fast Reports. I have been playing around with a lot of other things while I was trying to get that working and can't believe how versatile and easy to use it is. This is an outstanding product!

Leave a Comment