In the asp.net forums one user asked how to get Web.config's connection string value when using LINQ2SQL which is created inside another Class library project.
We know that if you use the System.Configuration classes, you will access the web.config from the application. Therefor, the first thing needed here is to reference the System.Configuration.dll inside the class library project (where Linq2Sql.dbml is placed).
Next, we need to add using System.Configuration namespace.
After we are done with it, the next thing which you need to do is just to use the overloaded constructor when creating instance of the DBML's Data Context
I have DBML with name DataClasses1
So, I can do this:
DataClasses1DataContext dc = new
which as parameter I have added the connection string from the Web.Config file.
Another way would be to modiy the dbml designer.cs file so that in the constructor that has global::[Namespace].Properties.Settings.Default.[ConnectionStringName] you can add ConfigurationManager.ConnectionStrings["MySampleDBConnectionString"].ConnectionString.
However I don't like this approach much, the first one is better and
thats why we have overloaded constructor that accepts connection string
I hope this could be helpful to someone else out there.
Please let me do know your feedback.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?