Melanie
Melanie

Reputation: 9

"ExecuteScalar: Connection property has not been initialized."

I'm new to c# and currently I have some problems when I'm running this program without debugging.

This is the login page for my project. I've create a service-based database and I want to connect to the data which is username and password in the table 'Table' which is in the database. However, I've encountered an issue which is "ExecuteScalar: Connection property has not been initialized." when I'm running this code.

Can anyone help me with this?

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
        cmd.Parameters.AddWithValue("@userid", textBox1.Text);
        cmd.Parameters.AddWithValue("@password", textBox2.Text);
        object result = cmd.ExecuteScalar();

        conn.Open();
        string useridlogin = Convert.ToString(result);
        conn.Close();

        if (useridlogin != " ")
        {
            Home_Page homepage = new Home_Page();
            homepage.Show();
        }
        else
        {
            MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
        }
    }

Upvotes: 0

Views: 2868

Answers (2)

Mohd Aman
Mohd Aman

Reputation: 220

You have open connection after ExecuteScalar thats you are getting error, You must open connection befor ExecuteScalar try this code

private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        conn.Open();
        cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
        cmd.Parameters.AddWithValue("@userid", textBox1.Text);
        cmd.Parameters.AddWithValue("@password", textBox2.Text);
        object result = cmd.ExecuteScalar();

        string useridlogin = Convert.ToString(result);
        conn.Close();

        if (useridlogin != " ")
        {
            Home_Page homepage = new Home_Page();
            homepage.Show();
        }
        else
        {
            MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
        }
    }

In your code SQL query find connection. But you have open it after ExecuteScalar so this is giving you an error.

Upvotes: 0

Bhavik Jadav
Bhavik Jadav

Reputation: 53

I can see that you've executed the ExecuteScalar method before opening the SQL Database connection, resulting in error you're getting.

Open the connection before ExecuteScalar method, and you're done.

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\hp\documents\visual studio 2015\Projects\PersonalFinancialSoftware\PersonalFinancialSoftware\Login.mdf;Integrated Security=True");
    conn.Open();
    SqlCommand cmd = new SqlCommand();

    cmd.CommandText = "SELECT COUNT (*) FROM Table where UserID = @userid AND Password = @password";
    cmd.Parameters.AddWithValue("@userid", textBox1.Text);
    cmd.Parameters.AddWithValue("@password", textBox2.Text);
    object result = cmd.ExecuteScalar();

    string useridlogin = Convert.ToString(result);
    conn.Close();

    if (useridlogin != " ")
    {
        Home_Page homepage = new Home_Page();
        homepage.Show();
    }
    else
    {
        MessageBox.Show("Invalid ID or password, please try again!", "Info", MessageBoxButtons.OK);
    }
}

Upvotes: 1

Related Questions