System.NullReferenceException : object reference not set to an instance of an o

Posted by: desousa.alexandre23 on 8 February 2021, 3:14 pm EST

    • Post Options:
    • Link

    Posted 8 February 2021, 3:14 pm EST

    Hi,

    I have create a report on flexreport designer with fields I used from a database, directly set with the designer application, but since I removed this connection on flexreport to replace it on code, it doesn’t work, I get this error with the dataSource object “System.NullReferenceException : object reference not set to an instance of an object”. And i can’t find how to handle this, as the most of samples shows how to load a report yet connected with flexreport designer. I would like to set the connection from an external database in my code and to execute a query here to load after that the template. So my question is what I’m doing wrong, How can I do to create a link on a template created with an external database ?

    Here is the code I’ve make so far. Thank you in advance for helping me

    
    private void Window_loaded(object sender, RoutedEventArgs e)
            {
                string sql = "SELECT * FROM ARCHPOINTS INNER JOIN ARCHRONDES ON ARCHPOINTS.idronde = ARCHRONDES.idarchronde where idarchronde=14 ORDER BY DATEPOINT desc, IDPOINT ASC";
                string modelReportPath = @"C:\Users\desou\Desktop\compte-rendu-tournee.flxr";
                string nameModelReport = "Compte-rendu de tournée";
    
                C1FlexReport rep = new C1FlexReport();
    
                //load report definition
                rep.Load(modelReportPath, nameModelReport);
    
                // initialize DataSource
                DataSource ds = rep.DataSource;
                ds.Recordset = sql;
                ds.DataProvider = DataProvider.OLEDB;
                ds.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source =C:\Users\desou\Desktop\rondes.mdb";
                
                ViewerPane.DocumentSource = rep;
    
            }
    
    
  • Posted 9 February 2021, 8:32 am EST

    Hi,

    The cause of the exception seems to be that the DataSource property of the report is returning null because you have removed the data source and the report has no data source associated with it.

    You need to add a new data source named “Main” to the DataSource collection of the Report using the Add() method of the DataSources property and then assign this data source to the report.

    You also need to assign the SQL command to the RecordSource property and not the RecordSet property. The RecordSource property is used to get or set a string that specifies which table, stored procedure, or SQL command to use for retrieving the report data.

    Please refer to the sample attached.

    If you have any questions, please let us know.

    Regards.

    Avnish.

    FlexReportTest.zip

  • Posted 13 August 2024, 11:34 am EST

    Thanks! This helped me.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels