简体   繁体   中英

Button Click throws System.NullReferenceException

System.NullReferenceException: Object reference not set to an instance of an object.

When clicking the button, i recieve that error. .
Form1 Class:

private void button1_Click(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["modelConnectionString"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(connectionString);
        sqlConnection.Open();
        SqlCommand connect = new SqlCommand("SELECT COUNT(*) From Users WHERE UserName = @user AND Password = @pass", sqlConnection);
        SqlParameter username = connect.Parameters.AddWithValue("@user", userName.Text);
        SqlParameter password = connect.Parameters.AddWithValue("@pass", passWord.Text);

        if ((int)connect.ExecuteScalar() == 1)
        {
            accessPic.BackgroundImage = Res.Accepted;
        }
        else
        {
            accessPic.BackgroundImage = Res.Denied;
        }
        sqlConnection.Close();
    }

Form1.Designer

        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(95, 90);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 25);
        this.button1.TabIndex = 8;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);

Step through the code in the debugger to find which line is throwing the exception and which object is null.

Also, you should refactor the code to use using to ensure that the connection is disposed of once you're done with it. The existing code will not close the connection if an exception is thrown between the opening and closing of the connection. Here's how to do that:

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["modelConnectionString"].ConnectionString;
    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        sqlConnection.Open();
        using (SqlCommand connect = new SqlCommand("SELECT COUNT(*) From Users WHERE UserName = @user AND Password = @pass", sqlConnection))
        {
            SqlParameter username = connect.Parameters.AddWithValue("@user", userName.Text);
            SqlParameter password = connect.Parameters.AddWithValue("@pass", passWord.Text);

            if ((int)connect.ExecuteScalar() == 1)
            {
                accessPic.BackgroundImage = Res.Accepted;
            }
            else
            {
                accessPic.BackgroundImage = Res.Denied;
            }
        }
    }
}

I agree with Laurence

The connection string section should look like this:

<configuration> 
    <connectionStrings>
        <add name="modelConnectionString" connectionString="whatever" providerName="System.Data.SqlClient" />
    </connectionStrings>
 <!-- more stuff here-->

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