Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have problem with connection String, Error (Object reference not set an instance of an object) how to debug this please

My app.config
C#
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      <connectionStrings>
        <add name = "databasecon" connectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PCIT2\Desktop\Database.accdb"></add>
      </connectionStrings>
    </startup>
</configuration>


This the connection String

C#
string connStr = ConfigurationManager.ConnectionStrings["databasecon"].ConnectionString;
Posted
Comments
[no name] 29-Oct-15 0:38am    
Can you please post your full Error Message and stack trace ?

The problem is not the connection string.

A NullReferenceException is throw when you try to access an instance member of a given variable while this variable has not been initialized, or been disposed.

Somewhere in your code file, there is a line where you are using a variable of which you think it contains a value, but which in fact does not contain any value.

The number of the line is indicated at the top of the stack trace of the exception. Find this line, put a breakpoint on it, or on a few lines above, and launch your project in debug mode. Execution will pause on the breakpoint, and you will be able to execute the code line by line, checking at each step that the variables actually hold the values you expect them to hold. Find the variable that is null, it will give you (or us) some clues about the problem.
 
Share this answer
 
v2
Comments
Rencyrence 29-Oct-15 21:37pm    
Thank You!
I dont know if its just the way you've posted that info in your question, but, it appears as though your connectionStrings is part of/'under' startup - I dont think it should be - try moving it 'up' a level' or at least change this

XML
<startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />


to

XML
<startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>




Also make sure you have added a reference to the Configuration Dll ie System.Configuration
 
Share this answer
 

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



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