MDHenry
MDHenry

Reputation: 1

The configuration system failed to initialize, when using a local database

I'm trying to use input from a windows form app to enter into a localdb, however when I send the data to the database it returns "the config system failed to initialize".

try
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["database"].ConnectionString);

    SqlCommand cmd = conn.CreateCommand();person newPerson = new person(FirstNameBox.Text, phoneBox.Text, emailBox.Text, LastNameBox.Text);
    cmd.CommandText = @"INSERT INTO Person (FirstName, LastName, Email,Phone)
                        VALUES(@FirstName, @LastName, @Email, @Phone)";

    cmd.Parameters.AddWithValue("@FirstName", newPerson.getFirstName());
    cmd.Parameters.AddWithValue("@LastName", newPerson.getLastName());
    cmd.Parameters.AddWithValue("@Email", newPerson.getEmail());
    cmd.Parameters.AddWithValue("@Phone", newPerson.getPhone());

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    if (!ValidateForm())
       return;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, Application.ProductName,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

and this is the app.config part

<?xml version="1.0" encoding="utf-8" ?>

<configSections>
  <connectionStrings>
    <add name="database" 
         connectionString=" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matthew\Documents\Visual Studio 2012\Projects\Midterm\Midterm\Database1.mdf;Integrated Security=True"></add>
</connectionStrings>
</configSections>
<connectionStrings>
    <add name="Midterm.Properties.Settings.Database1ConnectionString"
        connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

Upvotes: 0

Views: 914

Answers (1)

Ryan Stecker
Ryan Stecker

Reputation: 818

The most obvious issue I'm seeing here is that your app.config is specifying a <connectionString> section, where it should be <connectionStrings> (note the s).

You may want to provide your entire app.config as there may be other syntax/configuration issues that prevent ConfigurationManager from loading your app.config.

Edit for updated app.config:

Your app.config file is incorrectly formatted. <configSections> shouldn't be the root element of the file (it should be <configuration>).

The MSDN documentation for ConfigurationManager.ConnectionStrings shows an example of what a app.config file should be formatted like.

But, in short, the <connectionStrings> element (and the other elements such as <startup>) should be a child element of <configuration>.

What your app.config should be like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    *** ADD YOUR <add> LINES HERE ***
  </connectionStrings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

Upvotes: 2

Related Questions