Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to import datatable from MySQL to datagrid wpf .

But error ('Null Reference exception was unhandled by user code 'Object reference not set to an instance of an object.' Pin) shows at this line code :

C#
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;



I think it's a configuration problem , but my connection information are correct !

What I have tried:

I followed the steps in this article :

www.codeproject.com/Tips/362436/Data-binding-in-WPF-DataGrid-control?msg=5389895#xx5389895xx

and this is my APP.config

XML
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <connectionStrings>
      <add connectionString ="SERVER=; DATABASE=; UID=; PASSWORD= ;" name="ConString"/>
    </connectionStrings>

</configuration>

I'm a beginner In WPF ,Thank you for any help :) Sorry for my bad English .
Posted
Updated 27-Apr-17 22:40pm
v5
Comments
CHill60 27-Apr-17 6:19am    
Two questions...
1. Is that < pre lang="xml"> in your question actually in your App.Config file or did that happen as a result of posting the file into the question?
2. Are you absolutely sure that is the line that throws the exception.
EM_Y 27-Apr-17 6:23am    
1. yes it's Xml in App.config
2. yes the error shows at this line :
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
CHill60 27-Apr-17 7:45am    
If you want to respond to a comment then use the  Reply  button next to it so that the poster is notified.

Well, you have changed the content of your post, so the answer to my first question was not "it's the xml in the app.config" - it should have been "that happened when I posted my code into CodeProject"

When I use your code with your app.config I am unable to reproduce the problem. Try putting a breakpoint on the next line and run the program; then in the Immediate Window of the IDE type ? ConfigurationManager.ConnectionStrings["ConString"].ToString() and let me know what is shown or what error occurs then.
EM_Y 28-Apr-17 3:20am    
I got zero error , but the debugging stopped at the same line :
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
EM_Y 28-Apr-17 3:40am    
Problem solved ,My mistake was that I created new APP.config while I already have one !
Thank you for your time and help , I will do my best to focus more :)


1 solution

Posting solution to close off post. OP has confirmed that the problem is resolved. A second copy of App.config was causing the problem.
 
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