Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this error when I debug this code. "ExecuteScalar: Connection property has not been initialized." What did I do wrong? I get this error here: int userExists = (int)sqlCmd.ExecuteScalar();. And if I am getting it there I will also get it here: int correctPassword = (int)sqlCmd.ExecuteScalar();

C#
protected void Page_Load(object sender, EventArgs e)
{
    TextBoxEA.Focus();
 
    if (!IsPostBack)
    {
        Session["counter"] = 0;    
    }
    else
    {
        
        Session["counter"] = Convert.ToInt32(Session["counter"]) + 1;
 
        (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString))
        {
            con.Open();
 
            string cmdStr = "Select count(*) from Table22 where EmailAddress=@TextBoxEA";
            SqlCommand sqlCmd = new SqlCommand(cmdStr);
            sqlCmd.Parameters.Add("@TextBoxEA", TextBoxEA.Text);
            int userExists = (int)sqlCmd.ExecuteScalar();
 
            cmdStr = "Select count(*) from Table22 where EmailAddress = @TextBoxEA AND Password=@TextBoxPW";
            sqlCmd = new SqlCommand(cmdStr);
            sqlCmd.Parameters.Add("@TextBoxEA", TextBoxEA.Text);
            sqlCmd.Parameters.Add("@TextBoxPW", TextBoxPW.Text);
            int correctPassword = (int)sqlCmd.ExecuteScalar();

            string msg = "";
            if (userExists == 0)
                msg = "alert('User Name Does Not Exist You Must Fill Out Registration First');";
            else if (correctPassword == 0)
                msg = "alert('Invalid UserName / Password');";
            else if (Convert.ToInt32(Session["counter"]) >= 3)
            {
                msg = "alert('The Account is Locked');";

                cmdStr = "Update Table22 SET isLocked = true where EmailAddress = @TextBoxEA";
                sqlCmd = new SqlCommand(cmdStr);
                sqlCmd.Parameters.Add("@TextBoxEA", TextBoxEA.Text);
                sqlCmd.ExecuteNonQuery();
            }
            if (msg.Length > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", msg, true);
                TextBoxEA.Text = string.Empty;
            }
             con.Close();
        }   

    }
}
Posted
Updated 3-May-18 7:58am

1 solution

Hello friend, try the following code changes:

C#
string cmdStr = "Select count(*) from Table22 where EmailAddress=@TextBoxEA";
            SqlCommand sqlCmd = new SqlCommand(cmdStr, con);
            sqlCmd.Parameters.Add("@TextBoxEA", TextBoxEA.Text);
            int userExists = (int)sqlCmd.ExecuteScalar();


You have missed to add the Connection object with the SqlCommand object.
 
Share this answer
 
v2
Comments
Computer Wiz99 28-May-14 11:43am    
Debabrete_Das, That fixed my login problem but the unsuccessful login attempts is not working. What did I do wrong in my code?
Debabrata_Das 28-May-14 11:53am    
Hi, good to hear that your first problem is resolved. Could you please be specific about your other problem that you are facing?
Computer Wiz99 28-May-14 15:56pm    
Sorry about the wait, when I test out the code the messages do pop up but when I enter in a username and the incorrect password three times the account does not lock. Why is that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900