Unpick LINQ to SQL Connection String from Application Settings

In my application i used Linq to Sql class i.e dbml class.In linq by default DataContext object read connection string from application setting and Stored this also in App.config as a reference copy from “Application Settings”.
However when I run the application from debug folder on my PC, it runs correctly, but when I change the connection string i.e server name of YourApplicationName.exe.config then try to run it an error message say that connection related problem bla bla….
So i think when i will deploy this client side then i will again face same problem.
But i have gotten solution for this problem.
If anybody follow this step then every problem will be solved.

1. Open up the LINQ to SQL designer, and open the Properties tab of the designer ,expand Connection and set Application Settings to False. Save.

2. Close that down and open up your DataContext designer file (dbml_name.designer.cs) and alter the DataContext constructor. You will immediately notice how your connection string decided to jump in here as you turned off application wide settings. So the part to focus on here is altering the base() inheritor. Renaming “MyConnString” below to suit your own. You will also need a reference to System.Configuration:
* public dbDataContext() : base(ConfigurationManager.ConnectionStrings[“MyConnString”].ConnectionString, mappingSource)

3. Open the App.config or Web.config featured in the project where your LINQ to SQL classes reside, and rename the connection string to what you defined as “MyConnString“.

4. You now must Cut the entire entry with name change and Paste it into the main App.config or Web.config of the application which is to access the data, such as a web application, Silverlight, WPF, WCF etc.

5. Now Right Click and open the Properties on your DAL or project containing your LINQ to SQL classes and remove the connection string “Application Setting” reference on the Settings tab.


ref:Get Connection String from app.config
If you have any query please leave a comment.


8 Responses to “Unpick LINQ to SQL Connection String from Application Settings”

  1. Dhaval Patel Says:

    Error 1 ‘DailyExapense.LoginClasses1DataContext’ does not contain a constructor that takes 0 arguments C:\Users\U0149043\Downloads\DailyExapense(1)\DailyExapense\DailyExapense\ViewModels\ForgotPassViewModel.cs 16 52 DailyExapense

    • Dhaval Patel Says:

      When i removed the connection string from ma classes properties then in my dbml_name.designer.cs have two arguments first is connection and second one is mappingSource so upper error will displayed.

      • refat38 Says:

        Actually this post is clumsy to me and i have not enough tome to narrow down.
        However Please follow the quickest way
        1.Open up the LINQ to SQL designer, and open the Properties tab of the designer ,expand Connection and set Application Settings to False. Save.
        1.Add a reference System.Configuration.
        2,when u delete connection string from your property now edit this constructor by
        public dbDataContext() : base(ConfigurationManager.ConnectionStrings[“MyConnString”].ConnectionString, mappingSource)
        here MyConnString is comes from your app.config.
        now build and run.

  2. Dhaval Patel Says:

    Thank You So much it’s done…

  3. Dhaval Patel Says:

    After Doing these my user control not loaded it gives me error like “object reference not set to an instance of an object” can you please help me out of these..

  4. Nida Says:

    These are in fact fantastic ideas in concerning blogging.
    You have touched some nice points here. Any way keep up wrinting.

  5. Johng199 Says:

    betamethasone acetatena phos kefdedkkadcf

  6. Johng109 Says:

    Would you be taken with exchanging links? dfgkbeegkeaf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: