Ticket T285429
Visible to All Users

How to use SqlDataSource with ReportsV2 and pass the application connection string into the data source?

created 10 years ago

Hello,
I have created a report within visual studio and it creates a connection string in the app.config file in the main module, however i would like the report to pick the application connection string at run time because in production environment the server connection strings are dynamic and keeps on changing depending on the location of the database. I have followed instructions given in support Ticket T19225 and implemented ConfigureDataConnection event in the SqlDataSource. However when i run the application i still get an error of a missing connection as shown in the attached document. Secondly the Modifier Property is not available when defining report parameters while creating a report at run time therefore its not possible to set the parameters as public if they are to be used for sub report. I have attached a sample application for your review. in the sample the database is in the database folder and the error is in the error folder.

Thank you

Answers approved by DevExpress Support

created 10 years ago (modified 10 years ago)

Hello Stephen,

I suppose that you meant the T192225 ticket. The ConfigureDataConnection event suggested there can be used to change connection parameters, but cannot change the connection name that is attempted to be loaded from the configuration file. As a workaround, you can add a connection string with the "localhost_sampledb_Connection" name to the configuration file. As I see, you already did this, so when I test your reports, they are shown without errors.
You can also change the connection name and connection parameters using the ReportDataSourceHelper.CustomSetupReportDataSource event provided by XAF. In this case, you will not need to declare a connection string with the "localhost_sampledb_Connection" name in the configuration file. Here is an example:

Visual Basic
Public Class ReportDataSourceController Inherits WindowController Public Sub New() TargetWindowType = WindowType.Main End Sub Private _ReportsModuleV2 As ReportsModuleV2 Protected Overrides Sub OnActivated() MyBase.OnActivated() _ReportsModuleV2 = ReportsModuleV2.FindReportsModule(Application.Modules) If _ReportsModuleV2 IsNot Nothing Then AddHandler _ReportsModuleV2.ReportsDataSourceHelper.CustomSetupReportDataSource, AddressOf OnCustomSetupReportDataSource End If End Sub Private Sub OnCustomSetupReportDataSource(ByVal sender As Object, ByVal e As CustomSetupReportDataSourceEventArgs) If TypeOf e.Report.DataSource Is SqlDataSource Then CType(e.Report.DataSource, SqlDataSource).ConnectionName = "ConnectionString" End If For Each subreport As XRSubreport In e.Report.AllControls(Of XRSubreport)() If subreport.ReportSource IsNot Nothing Then _ReportsModuleV2.ReportsDataSourceHelper.SetupReportDataSource(subreport.ReportSource, Nothing, False, Nothing, False) End If Next subreport End Sub Protected Overrides Sub OnDeactivated() MyBase.OnDeactivated() If _ReportsModuleV2 IsNot Nothing Then RemoveHandler _ReportsModuleV2.ReportsDataSourceHelper.CustomSetupReportDataSource, AddressOf OnCustomSetupReportDataSource _ReportsModuleV2 = Nothing End If End Sub End Class

In addition, please note that we provide our own data sources integrated with XAF by default - CollectionDataSource and ViewDataSource. See how to use them in the Create a Report in Visual Studio topic.

I have created a separate ticket for your second question and will post my answer there: Cannot set report parameters' access modifier in the Report Designer at runtime.

    Comments (2)
    ST ST
    SoftClans Technologies 10 years ago

      Hello Anatol,
      I have used the data source integrated with XAF and it has worked as I had wanted, Thanks for your help
      Stephen

      Anatol (DevExpress) 10 years ago

        You are welcome!

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.