Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object. - App.config

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.

like image 674
bucketblast Avatar asked Apr 21 '13 13:04

bucketblast


People also ask

What is an object reference C#?

In C#, classes and interfaces are reference types. Variables of reference types store references to their data (objects) in memory, and they do not contain the data itself. An object of type Object , string , or dynamic is also a reference type. Reference Fundamentals.

How do I fix object reference not set to an instance?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

What is object reference not set to an instance of an object in C#?

So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.


1 Answers

you need to read AppSettings key as below ,

string connectionString = 
      ConfigurationSettings.AppSettings["MyDBConnectionString"];

still you receive empty value, try below steps

  1. Select the App.Config file in the solution explorer
  2. In the property window select Copy to Output Directory to Copy Always.
  3. 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>
like image 169
Damith Avatar answered Oct 26 '22 18:10

Damith