Click here to Skip to main content
Rate this: bad
good
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" ?>
<configuration>
  <connectionStrings>
 
    <clear/>
    <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;"/>
    
  </connectionStrings>
</configuration>

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.
Source=LoadTrackingTool.

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

Thnx,
Aduu
Posted 11-Nov-12 14:02pm
Aduu418
Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx[^],
http://msdn.microsoft.com/en-us/library/c6t3b6f3.aspx[^].
 
—SA
  Permalink  
Comments
Abhinav S at 12-Nov-12 0:36am
   
My 5.
Sergey Alexandrovich Kryukov at 12-Nov-12 0:58am
   
Thank you, Abhinav.
--SA
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 428
1 Maciej Los 260
2 Sergey Alexandrovich Kryukov 220
3 DamithSL 208
4 Dave Kreskowiak 200


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid