Search code examples
c#.netapp-configsqlconnection

app.config exception SqlConnection without connection string


Here is my problem: I have a simple C# console app and I open my connection with

SqlConnection conn = new SqlConnection("Data Source=server;" + "Initial Catalog=database;" + "User id=sa;" + "Password=pass;timeout=60;");

That works fine. I added the app.config with some keys in the AppSettings. That works fine too.

But if I add a new tag to the config, the previous code gives me exception...why?!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="template" value="value"/>
    <add key="saveas" value="value"/>
  </appSettings>
  <tag>

  </tag>
</configuration>

Solution

  • You cannot just add a random tag to your config file. The config file is parsed by the runtime and the runtime has to understand it.

    If you want to do something with your custom tag, it might be helpful to ask a question about that (make sure you search existing questions before you do), because there are different ways to reach what you want (for example custom configuration sections). But until then, the quickest solution to make your problem go away is to simply delete the tag tag from your configuration file.