set connectionstring based on log in RRS feed

  • Question

  • User168863485 posted

    I have a common connection string problem that I need some assistance with.

    In my web application, I have multiple clients, each with their own database.  I determine who the client is based on the URL. For example, www.website.com/Client1 or www.website.com/testclient.  Once I determine the client, I need to set their connection string. I have multiple connect strings in a connection.config file that I reference in my web.config file.

    How do I set all of my database connections to the proper connection string? I have seen some examples of session variables being used, but would like to avoid this.  I have a strongly typed dataset that all point to the default connection string:

    this._connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;

    Can I create a connection string in the global.asax, then change the above line to reference that connect string? Would that persist throughout the session, or do I have to add code to each user control to reference that connection string? For example, can I do something like this?

    ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["ConnectionString"]; in  global.asax

    this._connection.ConnectionString = cs.ConnectionString; in dataset;


    Tuesday, October 7, 2008 3:49 PM

All replies

  • User-1425666379 posted

    If you are using regular ASP.NET, you could do that in global.asax, or in a Master page. I would just read the config file for the right connection string each time the page loads.

    Am I getting you on this? 

    Also, are you using Dynamic Data or just regular ASP.NET?

    Tuesday, October 7, 2008 5:19 PM
  • User-330204900 posted

    Hi Soccerwiz, if this is DD your on to a looser here as the Model is setup in Application Start not Session Start [:'(]

    Hope this helps [:D]

    Tuesday, October 7, 2008 6:07 PM
  • User-1005219520 posted

    Hi Soccerwiz,

    If this is a generic ASP.NET question and not Dynamic Data specific can you try posting in  http://forums.asp.net/default.aspx?GroupID=16 ?

    I would write a method that passes the request (URL) and returns the connection string based on parsing the URL.

    Tuesday, October 7, 2008 6:19 PM
  • User168863485 posted

    Can I move this post, or do I have to repost in another section?


    Tuesday, October 7, 2008 6:56 PM
  • User-1005219520 posted

    Hey soccerwiz,

    I think you'll have to repost.

    Tuesday, October 7, 2008 6:59 PM