connectionString - change dynamically from parameter

connectionString - change dynamically from parameter

Forum member · Jul 13, 2007
I have a web-application that connects to different SQL DBs. DB connection string is stored in session and depends on Client account. I have ReportWriter reports (dplx) and in these reports I want to assign Query connectionString dynamically, from parameter. In "<Report>.aspx.vb" I added code: Parameters.Add("DefaultConnection", "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=XXXXX;Data Source=XXXXX") In "<Report>.aspx.dplx" I changed connection string code: connectionString="Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=XXXXX;Data Source=XXXXX" to: connectionString="#Parameters[&quot;DefaultConnection&quot;]#" Apparently this approach does not work, I'm getting an error message. Does anybody know the right way how to substitute connection string at a run time?

Re: connectionString - change dynamically from parameter

Support · Jul 13, 2007
Hello, You can specify the data source at run time by using the below sample code that accesses the connection string specified in the web.config file: Report rp = (Report)this.DocumentLayout.GetElementById("Report1"); rp.Query.ConnectionString = "dynamic connection string goes here" Thanks, ceTe Software Support Team

Re: connectionString - change dynamically from parameter

Forum member · Jul 13, 2007
Thank you for the fast response. Can you provide this example in VB.Net? I tried to convert from C# w/o success. My VB code is below: ___________________________________ Public Class Reports_ListOfAvailableLoans Inherits DplxWebForm Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ceTe.DynamicPDF.Document.AddLicense("XXXXXXX") ' Specify connection string Dim rp As Report = this.DocumentLayout.GetElementById("Report1") rp.Query.ConnectionString = "Password=XX;Persist Security Info=True;User ID=XX;Initial Catalog=XXXXXX;Data Source=XXXXXX" DrawPdfToWeb("Reports_ListOfAvailableLoans.pdf") End Sub End Class ___________________________________

Re: connectionString - change dynamically from parameter

Support · Jul 13, 2007
Hello, The VB.NET code would be as follows: Public Class Reports_ListOfAvailableLoans Inherits DplxWebForm Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim rp As Report = CType(Me.DocumentLay.GetElementById("Report1"), Report) rp.Query.ConnectionString = "Password=XX;Persist Security Info=True;User ID=XX;Initial Catalog=XXXXXX;Data Source=XXXXXX" DrawPdfToWeb("Reports_ListOfAvailableLoans.pdf") End Sub End Class Thanks, ceTe Software Support Team

Re: connectionString - change dynamically from parameter

Forum member · Jul 13, 2007
I'm getting an error message when I add: rp.Query.ConnectionString = "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=xxxxxx;Data Source=xxxxxx" Error: ______________________________ Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 13: ' Specify connection string ----------------------- Line 14: Dim rp As Report = CType(Me.DocumentLayout.GetElementById("Report1"), Report) Line 15: rp.Query.ConnectionString = "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=xxxxxx;Data Source=xxxxxx" Line 16: ' Specify connection string -------------------end- Line 17:

Re: connectionString - change dynamically from parameter

Support · Jul 13, 2007
Actually, I think I might have just seen something that was missed initially that may resolve the issue you are having. In your initial post you mentioned that in your aspx.vb page you were using: Parameters.Add("DefaultConnection", "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=XXXXX;Data Source=XXXXX") That looks correct. Then you mentioned that you were using replacing the connection string you had in your DPLX file with: #Parameters["DefaultConnection"]# That is not correct. Try using the following in the "Connection String" field for your DPLX file: #Global.AppSettings["DefaultConnection"]# I think that is all you will need to do to accomplish your initial goal. Let us know if that was it. Thanks, ceTe Software Support Team

Re: connectionString - change dynamically from parameter

Forum member · Jul 13, 2007
-------------- Try using the following in the "Connection String" field for your DPLX file: #Global.AppSettings["DefaultConnection"]# -------------- Nope, this does not work. Probably because for Global.AppSettings the "DefaultConnection" shoulf be defined in web.config file.

Re: connectionString - change dynamically from parameter

Forum member · Jul 16, 2007
Any other thoughts? If dplx-template conection string can not be changed dynamically at a run-time, then can I feed ReportWriter with dplx-template from a string? In other words if I can not change connection string at a run-time, then I'd like to build a string with dplx-template and proper connection string.

Re: connectionString - change dynamically from parameter

Support · Jul 16, 2007
Hello, You can change the query dynamically using the ConnectionString property of the Query class. Can you please send over the DPLX file and the code you are using to our [^a|mailto:support@cete.com|Support Team^] and we can take a look at why you are getting the NullReferenceException when using the line of code below: rp.Query.ConnectionString = "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=xxxxxx;Data Source=xxxxxx" We looked into it a little more and you cannot change the connection string dynamically using the parameters and referencing the parameter from the DPLX file. Thanks, ceTe Software Support Team

Re: connectionString - change dynamically from parameter

Forum member · Jul 16, 2007
I made it working by changing from: Dim rp As ceTe.DynamicPDF.ReportWriter.Report = CType(Me.DocumentLayout.GetElementById("Report1"), ceTe.DynamicPDF.ReportWriter.Report) to: Dim rpQuery As ceTe.DynamicPDF.ReportWriter.Data.Query = CType(Me.DocumentLayout.GetQueryById("Query"), ceTe.DynamicPDF.ReportWriter.Data.Query) This line changes connection for the report query: rpQuery.ConnectionString = "Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=xxxxxxxx;Data Source=xxxxxxx"