Click here to Skip to main content
13,293,950 members (57,595 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I am trying to access a MS Access database using Windows form. I added my connection string in App.congig file.

 <?xml version="1.0" encoding="utf-8" ?>
    <add name="Constr" providerName="System.Data.OleDb" 
         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\XXXX\\Documents\\YYYYY.accdb; Persist Security Info=False;"/>

Code I added in my code to read this is

string connectionstring = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;

but I am getting following error on this line.

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.

I am not getting why I am getting this error. Can anyone please help me out in this. I am new in this functionality.

Posted 11-Nov-12 15:02pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This kind of exceptions is one of the easiest to debug and fix. If happens, when you try to dereference some variable/member of some reference type by using one of the instance member of this type. Any such operations requires the reference to be non-null; If it's a null, this exception is thrown. In your case, ConfigurationManager.ConnectionStrings["Constr"] should be null. Check up the index value "Constr" — it could be misspelled, or a connection string under this name is not found in the configuration.

Please see:[^],[^].

Abhinav S 12-Nov-12 0:36am
My 5.
Thank you, Abhinav.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try to debug this error. In most likelihood, you are trying to access a property of an object that is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100