Click here to Skip to main content
12,633,336 members (32,814 online)
Rate this:
 
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 15: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 12-Nov-12 0:36am
   
My 5.
   
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 12 Nov 2012
Copyright © CodeProject, 1999-2016
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