Search code examples
c#datasourceoverwriteactivereports

How to overwrite datasource of rpx file for active reports in c#?


I have a rpx file with a datasource by default. I want to load it and check if the user wants to use a different datasource. I´ve already overwritten the datasource of the SectionReport object and execute a reader before to send it for loadlayout method. Do I need to map the fields with the textboxes or am I missing something for changing the datasource.


Solution

  • If the new datasource has different fields (columns) that the original datasource, then you'll need to add fields in the Fields collection of the report and also change the DataField property of the TextBoxes to the new fields in order to display new data. Take a look at the 'To use an Unbound data source' topic at the following documentation link: http://arhelp.grapecity.com/webhelp/AR11/index.html#BindReportstoaDataSource.html

    Edit: You can change just the connection string of the report's DataSource in the following way:

    SectionReport rpt = new SectionReport();
    XmlTextReader xtr = new XmlTextReader("../../Invoice1.rpx");
    rpt.LoadLayout(xtr);            
    
    var dataSource = rpt.DataSource as GrapeCity.ActiveReports.Data.OleDBDataSource;
    dataSource.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =C:\BILL.mdb; Persist Security Info = False";
    
    rpt.Run();
    
    viewer1.LoadDocument(rpt);