0

I am developing windows application in C# and i used sql server 2008 database.I have declared my connection string in app.config like this:

<connectionStrings>
      <add name="constr" connectionString="server=lexecute-pc;database=lexecute_window;integrated security=true"/>
      </connectionStrings>

And in my .cs page i am calling connection string like this:

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());

But when I run this page I got following exception:

Configuration system failed to initialize

Please help me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DK007
  • 285
  • 2
  • 6
  • 15
  • 1
    Your configuration file seems to be not valid xml or some config section is not defined in proper manner.. check the error message `Configuration system failed to initialize` you will get lot of answer to solve which might help – Harsh Baid Oct 25 '13 at 06:00

1 Answers1

2

Try using this, it is working fine for me:

 <add name="sConnectionString"
  connectionString="Integrated Security=SSPI;Persist Security Info=False;User ID=userid;Initial Catalog=databasename;Data Source=.\SQLEXPRESS"
  providerName="System.Data.SqlClient" />

  SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sConnectionString"].ConnectionString);

I hope it will help you.. :)

also you can use the website for connection strings: http://www.connectionstrings.com/sql-server/

Also make sure app.config starts with:

<?xml version="1.0"?>
<configuration>
 <configSections>
  <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
  <section name="yourProjectName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>

let me know if this help you. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24