简体   繁体   中英

Problem with SQL Connection exception on creating object

I created a SQL Server database in Visual Studio 2013.

I have a problem when I am trying to create a new SqlConnection object. The same code worked yesterday, but now I have some problem. I also added the SQLException but I can't reach this code

try
{
    string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();
}
catch (System.Data.SqlClient.SqlException sqlException)
{
    System.Windows.Forms.MessageBox.Show(sqlException.Message);
}

Application.Run(new LoginForm());

SqlConnection did not work. This is the exception. I don't know what it is wrong because it did work before

System.TypeInitializationException was unhandled
HResult=-2146233036
Message=The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
Source=System.Data
TypeName=System.Data.SqlClient.SqlConnection

StackTrace:

at System.Data.SqlClient.SqlConnection..ctor(String connectionString)

InnerException: System.InvalidOperationException
HResult=-2146233079
Message=Failed to read the configuration section for enclave providers. Make sure the section is correctly formatted in your application configuration file.

Error Message: Configuration system failed to initialize
Source=System.Data

StackTrace:

at System.Data.SqlClient.SqlConnection..cctor()

InnerException: System.Configuration.ConfigurationErrorsException
HResult=-2146232062
Message=Configuration system failed to initialize
Source=System.Configuration
BareMessage=Configuration system failed to initialize
Line=0

InnerException: System.Configuration.ConfigurationErrorsException
HResult=-2146232062
Message=Root element is missing. Source=System.Configuration
BareMessage=Root element is missing.

Line=0

InnerException: System.Xml.XmlException
HResult=-2146232000
Message=Root element is missing.
Source=System.Xml

app.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MercazApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="MyKey"
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Taekwon-Do\MercazApp\DB\LoginDb.mdf;Integrated Security=True;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <userSettings>
    <MercazApp.Properties.Settings>
      <setting name="username" serializeAs="String">
        <value />
      </setting>
      <setting name="password" serializeAs="String">
        <value />
      </setting>
      <setting name="name" serializeAs="String">
        <value />
      </setting>
    </MercazApp.Properties.Settings>
  </userSettings>
</configuration>

Root element is missing. (C:\\Users\\alexs\\AppData\\Local\\MercazApp\\MercazApp.exe_Url_ka1yz14eyfxx0cttxywliv5ag3ke50uk\\1.0.0.0\\user.config)

seems relevant. Likely, that file changed to something bad. Inspect it.

I can't believe you! I renamed that folder to old and after building it was created again and now it works!! I spent 4 hours with that. Where did you see the error and why it caused?

I simply read the exception in full. Exceptions contain a lot of information. It is worth acquiring a habit to fully read, understand and interpret it.

try putting the connection string directly into the code in place of the line that says:

string connectionString = ConfigurationManager.ConnectionStrings["MyKey"].ConnectionString;

This will tell you whether it's a web.config issue. Since it started playing up suddenly, its quite likely that some config change in IIS triggered the web config to be updated.

also worth checking whther there are any other .config files in different folders (including app_data) if you find one with no content, just delete it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM