Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my C# application, i am trying to connect to a DataConnection. The application is just a simple Login form but as soon as the user clicks the login button, it throws an exception error referencing the Connection String. Can anyone help?

public void userLoginSuccessfull()
{
    try
    {
        //////////////////////////////////
        // This line is throwing the error
        //////////////////////////////////
        string connString = System.Configuration.ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;

        if (txtUsername.Text != "" & txtPassword.Text != "")
        {
            string queryText = @"SELECT Count(*) FROM Users 
                     WHERE Username = @Username AND Password = @Password";

            using (SqlConnection cn = new SqlConnection(connString))
            using (SqlCommand cmd = new SqlCommand(queryText, cn))
            {
                cn.Open();
                cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
                int result = (int)cmd.ExecuteScalar();
                if (result > 0)
                {
                    loadUserForm();
                }
            }
        }
    }
    catch (Exception ee)
    {
            Console.WriteLine(ee.StackTrace);
    }
}

This is the error

'EnviroWaste Job Logger.vshost.exe' (CLR v4.0.30319: EnviroWaste Job Logger.vshost.exe): Loaded 'C:UserslistmDocumentsVisual Studio 2013ProjectsEnviroWaste Job LoggerEnviroWaste Job LoggerinDebugEnviroWaste Job Logger.exe'. Symbols loaded.

A first chance exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll

Configuration system failed to initialize

A first chance exception of type 'System.TypeInitializationException' occurred in System.Data.dll

The connection string looks like this:

connectionString="Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|UsersDatabase.mdf;Integr??ated Security=True"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

The error you got relates to a configuration file that has incorrect content. Most likely, your app.config (if desktop) or web.config (if web app) file the following error:

It does not have as its first element the <configSections> element.

Check that file and make sure it looks something like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- Section groups, and stuff like userSettings, etc  -->
    </configSections>

    <connectionStrings>
        <add name="connectionString" connectionString="Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|UsersDatabase.mdf;Integr??ated Security=True"/>
    </connectionStrings>

    <!-- Possibly other stuff  -->

</configuration>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[2] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...