Connection String

Posted by: darin on 22 October 2019, 3:21 am EST

    • Post Options:
    • Link

    Posted 22 October 2019, 3:21 am EST

    We have RPX files that in turn have SubReports on them. Both the main report and the sub-report need to have the same connection string. Currently, we are saving the connection string in the main report’s UserData. That works fine, until the server is upgraded and it now has a new name. Now, we have to go into every report to change the server name in the connection string stored in the UserData.

    How, via C# script, can i get the current connection string being used for the main report, so i can inturn use it for the sub report.

    Current C# script:

    string connstring=rpt.UserData.ToString();

    string strsql = “SELECT * FROM CustList”;

    GrapeCity.ActiveReports.Data.SqlDBDataSource m_ds = new GrapeCity.ActiveReports.Data.SqlDBDataSource();

    m_ds.ConnectionString = connstring;

    m_ds.SQL = strsql;

    GrapeCity.ActiveReports.SectionReport rptSub1 = new GrapeCity.ActiveReports.SectionReport();

    rptSub1.LoadLayout(“…\format\rCustList.rpx”);

    rptSub1.DataSource = m_ds;

    ((SubReport)rpt.Sections[“gList”].Controls[“sub1”]).Report = rptSub1;

  • Posted 22 October 2019, 10:03 pm EST

    Hello,

    You can use the same “connstring” value as the connection string to the subreport in the script.

    Also, the following statement helps you fetch the ConnectionString of the main report. This can be used for the subreport’s connection string:

    string connStr=(rpt.DataSource as GrapeCity.ActiveReports.Data.SqlDBDataSource).ConnectionString;
    
    

    Hope this helps.

    Regards,

    Esha

  • Posted 23 October 2019, 7:28 am EST

    What is totally strange is this works perfectly when doing the preview from the forms designer, but when running the exact same report we receive an Except has been thrown by the target of an invocation error, Object reference not set to an instance of an object.

    I am just trying to print the current connectionstring:

    System.Windows.Forms.MessageBox.Show((rpt.DataSource as GrapeCity.ActiveReports.Data.SqlDBDataSource).ConnectionString);

    That fails when running the report, but the preview button on the designer shows this message with no issues at all.

    the only thing i can think of is the program that runs our reports sets the DataSource to a SqlDataReader - not sure if that is the correct way, but that is how we have done it since 2002 with ActiveReports.

    I was able to get this to work by forcing the reports User Data to be the connection string in our program, and then it pulls the user data at runtime, but i just think it makes more sense to use the connection string from the current connection.

    Let me know if i doing something wrong.

    THanks.

  • Posted 23 October 2019, 10:29 pm EST

    Hello,

    Since the main report’s data source is SqlDataReader, it should be type-casted in a different manner:

    ((System.Data.SqlClient.SqlDataReader)this.DataSource)
    
    

    However, the Connection property corresponding to the same is protected and hence, the ConnectionString cannot be fetched with this.

    Thus, you would have to set the ConnectionString for the subreport using the same variable that’s used to set the main report’s ConnectionString. The other option is to use the UserData property, as you are aware of.

    Regards,

    Esha

Need extra support?

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

Learn More

Forum Channels