none
Dynamically Changing connection string in LINQ to SQL data context not working RRS feed

  • Question

  • DataContext context = new DataContext("portalConnectionString-Prod");

     context = new DataContext("portalConnectionString-Test");

    Is not working Why?

    Thursday, January 10, 2013 7:41 PM

Answers

  • Hi erchowdary;

    Please try it like this. You will need to add a reference to the dll file System.configuration.dll and the using statement:

    using System.Configuration;

    string connStr = ConfigurationManager.ConnectionStrings["portalConnectionString-Prod"].ConnectionString;
    DataContext context = new DataContext(connStr);
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Shyam Kr Friday, January 11, 2013 8:09 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 7:58 AM
    Thursday, January 10, 2013 9:25 PM

All replies

  • Hi erchowdary;

    Please try it like this. You will need to add a reference to the dll file System.configuration.dll and the using statement:

    using System.Configuration;

    string connStr = ConfigurationManager.ConnectionStrings["portalConnectionString-Prod"].ConnectionString;
    DataContext context = new DataContext(connStr);
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Shyam Kr Friday, January 11, 2013 8:09 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 7:58 AM
    Thursday, January 10, 2013 9:25 PM
  • When you passed portalConnectionString-Test as a paramtere to data context, it considered this string as a connection string.

    The problem why your DataContext didn't work is that it recieved incorrect string as a connection string. Pass the correct connection string as a value and everything will work fine.


    Please Mark as Reply and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.net/

    Friday, January 11, 2013 1:00 PM