Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi, I am trying to access a MS Access database using Windows form. I added my connection string in App.congig file.

XML
 <?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

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
 
Share this answer
 
Comments
Abhinav S 12-Nov-12 0:36am    
My 5.
Sergey Alexandrovich Kryukov 12-Nov-12 0:58am    
Thank you, Abhinav.
--SA
Try to debug this error. In most likelihood, you are trying to access a property of an object that is null.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900