0

I have the app.config file which contains the following code for my Windows forms application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
    </startup>
  <connectionStrings>
    <add name = "myConnection"
         connectionString = "Data Source=(LocalDB)\v11.0; AttachDbFilename = "C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf";Integrated Security=True"
         providerName = "System.Data.SqlClient" />
  </connectionStrings>
</configuration>

connectionString attribute is giving error. Using \\ instead of \ doesn't help. It states that white space required.

How can I fix this?

1 Answers1

0

Internal quotes need to be escaped with &quot;:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>
  <connectionStrings>
    <add name = "myConnection"
         connectionString = "Data Source=(LocalDB)\v11.0;&quot;AttachDbFilename=&quot;C:\Users\USER\Documents\visual studio 2012\Projects\AliceAoi\WindowsFormsApplication2\Database2.mdf&quot;; IntegratedSecurity=True"
         providerName = "System.Data.SqlClient" />
  </connectionStrings>
</configuration>

The issue is that the XML parser figured that your connection string ended after the \v11.0;", but it continues on and you get other errors. You have to remove the internal quotes with the &quot; escape sequence.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Wait, why did you add `"` before and after `AttachDbFilename`? That's not a valid SQL connection string. In fact, not a single one of those `&qout;` is needed – Camilo Terevinto Oct 17 '18 at 00:39
  • @CamiloTerevinto Unless it was a missing quote there, but doing that I was getting quite a few errors about "AttachDbFilename attribute not allowed". – Ron Beyer Oct 17 '18 at 00:42