set datasource from report(Start report event)

I load connections dynamically. I make sql command (select) on connaction in report (start report event) and i try set datasource from data after select.
Unfortunately get error:

FastReport.Utils.DataTableException: Data1: Table is not connected to the data. Register the data using Report.RegisterData method.
w FastReport.Data.TableDataSource.LoadData(ArrayList rows) w D:\WCFFastReport\ProjektFastRaportNET\FastRaport\FastReport Biblioteki\Data\TableDataSource.cs:wiersz 327
w FastReport.Data.DataSourceBase.LoadData() w D:\WCFFastReport\ProjektFastRaportNET\FastRaport\FastReport Biblioteki\Data\DataSourceBase.cs:wiersz 377

My code bellow:
      string sql = "select* from dokumenty where dok_id between 3000 and 4000";
      OdbcConnection con1 = new OdbcConnection(((Report)sender).Dictionary.Connections[0].ConnectionString);// connections load dynamically from externall application
      OdbcCommand cmd = new OdbcCommand(sql, con1);
      dt = new DataTable();
      dt.TableName = "Data1";
    
      using (OdbcDataAdapter ad = new OdbcDataAdapter(cmd))
      {
        ad.Fill(dt);
      }
 
      Text8.Text = dt.Rows.Count.ToString();
      tab = new TableDataSource();
    
      tab.Table = dt;
      tab.Name = "Data1";
      tab.Alias="Data1";
     
      Report.RegisterData(dt , "Data1");
      Report.GetDataSource("Data1").Enabled = true;  
    
      Data1.DataSource = tab;

How can register data?

Comments

  • edited November 2016
    All code in previous post is set in _StartReport event in report.
    I get error when i set
      Data1.DataSource = tab;
    

    Objects 'tab' and 'dt' are global object.

    Where the problem is ?
  • edited 10:55PM
    read FRNetProgrammerManual-en.pdf for registering data
  • edited November 2016
    I read instruction , but RegisterData(...) don't work in _StartReport event. Before Load(...) is work correctly.
  • edited 10:55PM
    follow the pattern as instructed by fastreport, never use event for registering data.

    in my case, when i click a button, execute this method :

    assuming desktop application and already declared fastreport previewcontrol (internal FastReport.Preview.PreviewControl PreviewControl1;)
            private void GLReportBS()
            {
                try
                {
                    using (OleDbConnection cnn = new OleDbConnection(ClassGlobal.mySQLConnString))
                    {
                        cnn.Open();
    
                        using (OleDbCommand cmd = new OleDbCommand())
                        {
                            cmd.Connection = cnn;
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = "SELECT * FROM qryRptBS";
    
                            using (OleDbDataReader dr = cmd.ExecuteReader())
                            {
                                if (dr.HasRows == false)
                                {
                                    throw (new Exception("There is no data for this report."));
                                }
                                using (DataTable dt = new DataTable())
                                {
                                    dt.Load(dr);
                                    dt.TableName = "MainReport";
    
                                    FastReport.Report report = new FastReport.Report();
                                    using (System.IO.Stream ms = new System.IO.MemoryStream(GL.Properties.Resources.rptBS))
                                    {
                                        //I put frx file as resources
                                        report.Load(ms);
                                    }
                                    report.RegisterData(dt, dt.TableName);
                                    report.FileName = "Balance Sheet";
                                    report.Prepare();
                                    PreviewControl1.AddTab(report, report.FileName);
                                    PreviewControl1.ZoomPageWidth();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    

    the question : who is reponsible to dispose fastreport class instance?
    FastReport.Report report = new FastReport.Report();

    if you click close button in fastreport previewcontrol, object report will be disposed.

    never ever worry about memory leaks.

    if you want to expose another report, create another instance :
    FastReport.Report report = new FastReport.Report();

    that's the answer about datasource not refreshing, create another instance of fastreport class.


Leave a Comment