In a C# 2008 application, I used linq to sql to connect to a sql server 2008 database. Now I want to obtain the connection of the database from the
app.config file instead of hardcoding the value into the application. I want to be able to change the connection string to the database since I will be moving this application from unit testing database to user acceptance database to the the prodction database.
Right now I am getting the error message, ""Object reference not set to an instance of an object." I know this error message is coming from the line of code that I listed below since it is the only line of code I changed.
The following is a snippet of the code from the *.designer.cs file (this is part of the dbml file).
public esampleDataContext() :
**Note: base(global::sample.Properties.Settings.Default.DEVConnectionString, mappingSource) is the line of code that I replaced that was generated by the
I want to mention the following:
1. The *.dbml file is associated with a particular project file.
2. The connection string settings for this *.dbml file are the following:
a. DEVConnectionString (settings)
b. Connection String = Data Source=dev1;Initial Catalog=DEV_test;Integrated Security=True
c. application settings=True
d. settings property name is DEVConnectionString.
3. The project file that has the *.dbml file. has a reference set to the system.configuration.
**Note: This code in similar applications that I wrote, but I do not why it does not work here.
Thus can you tell me, show me in code, and/or point me to a reference that I can use so I can obtain the value in the app.config file for the database connection string I want to use. I do not want to use a hard-coded connection string to the database.
point me to a reference that I can use so I can obtain the value in the app.config file for the database connection string I want to use. I do not want to use a hard-coded connection string to the database.
Correct me if I'm wrong, but isn't the connectionstring in the app.config file? Isn't the app.config file an XML file that can be edited?
If both answers are yes, then your connectionstring isn't hardcoded, but loaded from the settings-file.
The problem is the settings-file does not get the value from the app.config file. The values are retained in the settings-file from the time I orginally created the app.config file. The settings-file needs to change at some point.
Your app.config needs to be set up in a very specific way for that to work.
If it isn't set up that way then ConnectionStrings will be null.
I would suggest that you do the following
1. Create a project/app that ONLY gets the connection string
2. Create a app.config that ONLY has that connection string.
3. Verify in that app that ConnectionStrings is not null before using it.
4. Get the connection string property and print it.
Then if that doesn't work then you can post the entire class and the entire app.config. Do not include real info in the app.config all you care about is that it get a value not what is in it.
Last Visit: 31-Dec-99 18:00 Last Update: 17-Apr-14 7:51