Object reference not set to an instance of an object. - App.config
Solution 1
you need to read AppSettings key as below ,
string connectionString =
ConfigurationSettings.AppSettings["MyDBConnectionString"];
still you receive empty value, try below steps
- Select the App.Config file in the solution explorer
- In the property window select Copy to Output Directory to Copy Always.
- Now Build the application and try again.
to access like below you need to add connectionStrings section in app config
string connectionString =
ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
sample app config
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\...\Database1.mdb"/> </connectionStrings> </configuration>
Solution 2
Check if you have put the app.config file under your startup project or not. In my case, I just had to put the app.config file under my startup project, and problem was solved.
Solution 3
This happened to me in class library with a console app set to run for debugging.
It is necessary to add the connection to the app.config in the console app, as it will not find it in your library if you're starting it from an outside process.
Solution 4
This:
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
is your problem.
You're accessing ConfigurationManager.ConnectionStrings
to get to your configuration item, but in the App.Config
file you've put it under appSettings
which is a different section of the config file than ConnectionStrings
You could either put your connection string in the relevant ConnectionStrings
section of the app.config (accessible with ConfigurationManager.ConnectionStrings
, or access it against the appSettings
section.
See:
http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx
For MSDN guidelines on storing connection strings.
bucketblast
I have interest in business intelligence, crystal reports, ms sql server, as well as access and excel.
Updated on March 23, 2020Comments
-
bucketblast about 4 years
I receiving the error and in the local window I am seeing for both conSettings and connectionString value of null. I am right to say ConfigurationManager is null and I need to create a new object. Maybe I am using Access and perhaps I have missed something in App.config file. Can someone help me on how to solve this problem, please. Thanks in advance.
App.config file...
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\...\Database1.mdb"/> </appSettings> </configuration>
Form.cs file...
private void btnShow_Click(object sender, EventArgs e) { ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"]; string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here try { con = new OleDbConnection(connectionString); con.Open(); cmd = new OleDbCommand("SELECT * FROM Table1", con); objReader = cmd.ExecuteReader(); while (objReader.Read()) { txtID.Text = ds.Tables[0].Rows[rno][0].ToString(); CBAgeGroup.Text = ds.Tables[0].Rows[rno][1].ToString(); CBGender.Text = ds.Tables[0].Rows[rno][2].ToString(); CBCrimOffen.Text = ds.Tables[0].Rows[rno][3].ToString(); if (ds.Tables[0].Rows[rno][4] != System.DBNull.Value) { photo_aray = (byte[])ds.Tables[0].Rows[rno][4]; MemoryStream ms = new MemoryStream(photo_aray); pictureBox1.Image = Image.FromStream(ms); } txtCV.Text = ds.Tables[0].Rows[rno][5].ToString(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } }
I have been advised to use App.config.
VS 2010 C# MS Access 2003
UPDATE 1 My App.config now looks like this...
<configuration> <ConnectionString> <add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Raj\Education\C_Sharp\Test1\Database1.mdb"/> </ConnectionString>
I am now receiving error of..."Configuration system failed to initialize". I am looking at it now on Google.
Update 2 Tried...
<configuration> <connectionStrings> <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\...\Database1.mdb"/> </connectionStrings> </configuration>
Receiving error of "Object reference not set to an instance of an object" Googling again
Update 3
<configuration> <connectionStrings> <clear /> <add name="MyDBConnectionString" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Source=\Database1.mdb" /> </connectionStrings>
With the update 3 I am receiving error the same error. I have included the Add reference System. Configuration and I have referenced using System.Configuration;
Conclusion
Perhaps it maybe there is a technical gitch between VS 2010 and Access 2003. I shall not use App.config this time round. I know there will be no problem with SQL Server. So I will leave it that. Thanks Damith and Clint for your time.
-
Clint about 11 yearsThat would work, but Microsoft have the connection strings section of the app.config for a reason and their official guidelines say that section ought to be used for connection strings.
-
bucketblast about 11 yearsI have done what you have advised and I am getting error of Configuration system failed to initialize. I am looking searching on the Goggle to find out what it is
-
bucketblast about 11 years@Damith - I have tried conntectionString and I am receiving error message of "Object reference not set to an instance of an object". Please can you advise me?
-
Clint about 11 years@bucketblast follow the example in the MSDN guidelines exactly, it could be you've not got the details in the connectionstring section of the file quite right.
-
Damith about 11 years@bucketblast are you sure, you set app config copy to out put directory always ?
-
bucketblast about 11 years@Damith - Yes I made sure of that. I am using MSDN guidelines given my Clint
-
mdegges over 6 yearsWhoops! In my case, I was trying to use web.config instead of app.config in my web form stub.