0

I have replaced the bottom line with the code that is in comments to make the location dynamic but it doesn't work. I have also changed the app.config file accordingly but still no hope. Could someone please guide me.

    // public string str = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\b5012622\Desktop\Jan_Prototype\DB.mdf;Integrated Security=True";

    public string str = ConfigurationManager.ConnectionStrings["connectionstrings"].ConnectionString;

Here is the app.config file

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <connectionstrings>
<add name="dbConnection">
  connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\DB.mdf;Integrated Security=True;User Instance=True"
  providerName="System.Data.SqlClient"
</add>
</connectionstrings>
 <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>  
</configuration>
jarlh
  • 42,561
  • 8
  • 45
  • 63

2 Answers2

0

Put proper connection string name mentioned at name attribute in connectionstrings element:

public string str = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Johan Shen
  • 158
  • 6
  • http://imgur.com/a/W9Awm Thanks for that. It did fix the issue I was having with schemas. But I am still getting this error. – URAISH AHMED Jan 27 '17 at 04:44
  • http://stackoverflow.com/questions/6436157/configuration-system-failed-to-initialize – Johan Shen Jan 27 '17 at 04:50
  • With the current solution and using my own app.config file. I get NullExceptionError. If I follow the link given by Johan Shen and follow the instructions I get configurationException error. Which one should I go for and any suggestions? – URAISH AHMED Jan 27 '17 at 05:05
  • OK, i find the question. – Johan Shen Jan 27 '17 at 07:20
0

You should set your connectionstring as attribute of add element instead of the value of add element

Tim Ng
  • 1
  • 1