0

I tried to get appsetting from app config in my winform application (just a small project for study) but when i run my project, vs2013 throws an exception named "NullReferenceException"

Although i have read this topic : how to get connection string from app config in c# And my code now is :

SqlConnection bookcon = new SqlConnection(ConfigurationManager.AppSettings["MyConnection"].ToString());

Im still receive the same exception, please tell me how to solve this problem? This is my appconfig file :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyConnection" value="Data Source=HUYMANIT\\SQLEXPRESS;Initial Catalog=QLTV_1312240;Persist Security Info=True;User ID=sa;Password=123456"/>
</appSettings>
</configuration>
Community
  • 1
  • 1
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 11 '15 at 03:25
  • `AppSettings["MyConnection"]` return string. Remove `ToString()` method.. – Iswanto San May 11 '15 at 03:26
  • You are getting the standard NullReferenceException because the "MyAccount" does not exist in the AppSettings list. Even though you have added it to your App.config file, can you confirm your App.config file actually exists in your build directory (i.e. bin/debug/you.app.exe.config)? – Juzzbott May 11 '15 at 05:18
  • Also, as it's a ConnectionString, why not add it to the element instead? – Juzzbott May 11 '15 at 05:21

0 Answers0