Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Connectionstring depending on logged in user

Hi,

I have an asp.net site that hosts codes for several different organisations. I would like to be able to form the connection string differently depending on the user that is logged in, since each organisation has their own database, but uses the same asp.net code.

For example a connection string can look like this for Organisation A:
 <add name="dbConn" connectionString=" Data Source=.\SQLEXPRESS;Initial Catalog=db1DBLocal;Integrated Security=True" providerName="System.Data.SqlClient" />

Open in new window


And for Organisation B:
 <add name="dbConn" connectionString=" Data Source=.\SQLEXPRESS;Initial Catalog=db2DBLocal;Integrated Security=True" providerName="System.Data.SqlClient" />

Open in new window


It's important that the name of the connection is the same but that the catalog can be altered.

Worth mentioning is that I also in the current system has added a class called config.vb. In that class I have put the following code to easily be able to use the connection string in my code behind pages:
 Public Shared ReadOnly Property ConnectionString() As String
        Get
            Return ConfigurationManager.ConnectionStrings("dbConn").ConnectionString     
        End Get
    End Property

Open in new window


So if the solution in some way can be compatible with this waqy of working it would be great!

I have two parameters called systemID and one called systemDatabase as profiles in the system and those parameters decides what system and what database the user belongs to.

If someone has a good solution to how I easily can solve this I would be most grateful.

Thanks for help!

Peter
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Well you need to be able to change following

 Public Shared ReadOnly Property ConnectionString() As String
        Get
            Return ConfigurationManager.ConnectionStrings(OrganisationSpecificConnectionStringName).ConnectionString    
        End Get
    End Property


How you get that OrganisationSpecificConnectionStringName depends on how you link your logged in user to organisation.
Avatar of LordWabbit
LordWabbit

A minor change in your config
Return ConfigurationManager.ConnectionStrings("dbConn").ConnectionString .Replace("db1DBLocal", "db2DBLocal")
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial