Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: App.config
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 at 12-Nov-12 0:36am
My 5.
Sergey Alexandrovich Kryukov at 12-Nov-12 0:58am
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
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851

Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2014
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