What am trying to create is a single login screen that connects the users to different databases depending on a certain code that each user has.
i have created some keys in my config file which corresponds to the user codes as follows
<appSettings>
<add key="ch001" value="h001"/>
<add key="ch002" value="h002"/>
</appSettings>
Then i have created connections string as follows
<connectionStrings>
<add name="Dbconn_h001" connectionString="XXX" providerName="XXX"/>
<add name="Dbconn_h002" connectionString="XXX" providerName="XXX"/>
</connectionStrings>
Then i have created a class to get the key value corresponding to the connection string as follows
Imports System.Web.Compilation
Imports System.CodeDom Imports System.ComponentModel
Public Class ConnStringExpressionBuilder Inherits ExpressionBuilder
Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object
Return System.Configuration.ConfigurationManager.ConnectionStrings("Dbconn_" & System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString))
End Function
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
Dim type1 As Type = entry.DeclaringType
Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
Dim expressionArray1(2) As CodeExpression
expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
expressionArray1(1) = New CodeTypeOfExpression(type1)
expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType()), "GetEvalData", expressionArray1))
End Function
End Class
The issue is
System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString)
returns a null reference