Thanks for the prompt response. I think I have it working!
I changed the CodeBlocked to CodeExpression and it seems to work:
If CBool(pair1.Second)
Then
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType),
"GetConnectionString",
New CodeExpression() {New CodePrimitiveExpression(text1)})
End If
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType),
"GetConnectionStringProviderName",
New CodeExpression() {New CodePrimitiveExpression(text1)})
I don't know the significance of CodeBlocked so I may have screwed something up! However, I have successfully changed a connecting string and back again on a test page. I am getting an error sometimes pointing to line 44:
GetConnectionStringSettings().Add(settings)
"The entry 'WebTool1ConnectionString' has already been added"
However, I suspect this is because I am doing this in Page_PreInit on my test page (and noting your caveat!) I will test more fully in the way I actually want to use it (setting the connection string once for the entire session when the user logs in). I'll
let you know how I get on.
I am using VisualStudio 2005 with ASP.Net 2 and using VB.Net, SQL Server 2005.
Sorry James, I meant to include my amended version of the CodeBlocked lines:
If CBool(pair1.Second)
Then
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType),
"GetConnectionString",
New CodeExpression() {New CodePrimitiveExpression(text1)})
End If
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType),
"GetConnectionStringProviderName",
New CodeExpression() {New CodePrimitiveExpression(text1)})
Not sure what the CodeBLOCKED stuff is all about (or why you changed it!!!)... so can't help there!
The error you're getting seems a little weird too... it looks like it is moaning because you've already added the connection string... but you are allowed to do that... it simply overwrites the connection string for the name! So again... don't know... you
could try putting a try catch round that line... and then a bit of debugging!
This is really confusing and difficult to discuss because the forum software here keeps changing the code snippets that I post. Below is a part of YOUR code as you originally posted it. This gives a compilation error aound the "New CodeBLOCKED EXPRESSION",
as reported above.
#Region "Main ConnectionStringsExpressionBuilder Code"
' pulled from ConnectionStringExpressionBuilder class... just change how we get the connection strings collection!
Public Overrides Function GetCodeExpression(ByVal entry As System.Web.UI.BoundPropertyEntry, ByVal parsedData As Object, ByVal context As System.Web.Compilation.ExpressionBuilderContext) As System.CodeDom.CodeExpression
Dim pair1 As Pair = DirectCast(parsedData, Pair)
Dim text1 As String = CStr(pair1.First)
If CBool(pair1.Second) Then
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionString", New CodeBLOCKED EXPRESSION {New CodePrimitiveExpression(text1)})
End If
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionStringProviderName", New CodeBLOCKED EXPRESSION {New CodePrimitiveExpression(text1)})
End Function
I got around this by changing the codeBlocked part to just
CodeExpression with () after it and this now seems to work (sorry but the forum won't let me actually type it exactly!)
I wonder if the ASP.Net Forums software is detecting what it thinks is dangerous code being posted and is therfore BLOCKING it???? Please look at your original code as you pasted it and confirm you can see "New CodeBLOCKED EXPRESSION". Is this what your
original source code looked like?
Yes, everything appears to be working fine after making that change. I am still testing but so far the ConnectionString is correctly changed based on the user logon and I have applied the markup changes (basically a simple 'change all' edit applied to the
connection strings) to one of my more complicated pages and it appears to work perfectly. I don't pretend to understand what every line of your code does but I understand the approach and am REALLY grateful for all your help. Once you have a better place to
post the code I will pass on the link to other people who are struggling with this if that is OK with you?
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 12:05 PM|LINK
Hi James,
Thanks for the prompt response. I think I have it working!
I changed the CodeBlocked to CodeExpression and it seems to work:
If CBool(pair1.Second) ThenReturn New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionString", New CodeExpression() {New CodePrimitiveExpression(text1)})
End If
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionStringProviderName", New CodeExpression() {New CodePrimitiveExpression(text1)})
I don't know the significance of CodeBlocked so I may have screwed something up! However, I have successfully changed a connecting string and back again on a test page. I am getting an error sometimes pointing to line 44: GetConnectionStringSettings().Add(settings)
"The entry 'WebTool1ConnectionString' has already been added"
However, I suspect this is because I am doing this in Page_PreInit on my test page (and noting your caveat!) I will test more fully in the way I actually want to use it (setting the connection string once for the entire session when the user logs in). I'll let you know how I get on.
I am using VisualStudio 2005 with ASP.Net 2 and using VB.Net, SQL Server 2005.
Thanks again,
Cliff
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 12:12 PM|LINK
Sorry James, I meant to include my amended version of the CodeBlocked lines:
If CBool(pair1.Second) Then
Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionString", New CodeExpression() {New CodePrimitiveExpression(text1)}) End If Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionStringProviderName", New CodeExpression() {New CodePrimitiveExpression(text1)})Cliff
James_2JS
Participant
1526 Points
335 Posts
Re: Dynamic connection strings
Feb 12, 2007 12:53 PM|LINK
Not sure what the CodeBLOCKED stuff is all about (or why you changed it!!!)... so can't help there!
The error you're getting seems a little weird too... it looks like it is moaning because you've already added the connection string... but you are allowed to do that... it simply overwrites the connection string for the name! So again... don't know... you could try putting a try catch round that line... and then a bit of debugging!
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 02:04 PM|LINK
James,
This is really confusing and difficult to discuss because the forum software here keeps changing the code snippets that I post. Below is a part of YOUR code as you originally posted it. This gives a compilation error aound the "New CodeBLOCKED EXPRESSION", as reported above.
#Region "Main ConnectionStringsExpressionBuilder Code" ' pulled from ConnectionStringExpressionBuilder class... just change how we get the connection strings collection! Public Overrides Function GetCodeExpression(ByVal entry As System.Web.UI.BoundPropertyEntry, ByVal parsedData As Object, ByVal context As System.Web.Compilation.ExpressionBuilderContext) As System.CodeDom.CodeExpression Dim pair1 As Pair = DirectCast(parsedData, Pair) Dim text1 As String = CStr(pair1.First) If CBool(pair1.Second) Then Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionString", New CodeBLOCKED EXPRESSION {New CodePrimitiveExpression(text1)}) End If Return New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetConnectionStringProviderName", New CodeBLOCKED EXPRESSION {New CodePrimitiveExpression(text1)}) End FunctionI got around this by changing the codeBlocked part to just CodeExpression with () after it and this now seems to work (sorry but the forum won't let me actually type it exactly!)
I wonder if the ASP.Net Forums software is detecting what it thinks is dangerous code being posted and is therfore BLOCKING it???? Please look at your original code as you pasted it and confirm you can see "New CodeBLOCKED EXPRESSION". Is this what your original source code looked like?
Thanks,
Cliff
James_2JS
Participant
1526 Points
335 Posts
Re: Dynamic connection strings
Feb 12, 2007 02:47 PM|LINK
Aha!!! It's all making sense now!!! Looks like you are indeed right!! There is no CodeBLOCKED in my original code!!
It obviously doesn't like that!!!! So when you change it, does everything work OK for you??
I'm going to find somewhere to post the source code and create a link to it from there... will get on the case tonight!!
Cheers,
James
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 03:23 PM|LINK
Yes, everything appears to be working fine after making that change. I am still testing but so far the ConnectionString is correctly changed based on the user logon and I have applied the markup changes (basically a simple 'change all' edit applied to the connection strings) to one of my more complicated pages and it appears to work perfectly. I don't pretend to understand what every line of your code does but I understand the approach and am REALLY grateful for all your help. Once you have a better place to post the code I will pass on the link to other people who are struggling with this if that is OK with you?
Many, many thanks!
Cliff
James_2JS
Participant
1526 Points
335 Posts
Re: Dynamic connection strings
Feb 12, 2007 03:45 PM|LINK
No worries at all Cliff!! Glad you're all sorted. I'll drop a post once it's finished!
Cheers,
James
James_2JS
Participant
1526 Points
335 Posts
Re: Dynamic connection strings
Feb 12, 2007 10:44 PM|LINK
Hi Cliff,
Have a look here:
http://2jsltd.co.uk/
NB this is just a temporary location for it!!!
Cheers,
James
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 11:18 PM|LINK
CliffMitchel...
Member
147 Points
47 Posts
Re: Dynamic connection strings
Feb 12, 2007 11:18 PM|LINK
Excellent!
Cliff