ExecuteScalar:尚未初始化Connection属性。 [英] ExecuteScalar: Connection property has not been initialized.

查看:424
本文介绍了ExecuteScalar:尚未初始化Connection属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试此代码时出现此错误。 ExecuteScalar:连接属性尚未初始化。我做错了什么?我在这里得到这个错误:int userExists =(int)sqlCmd.ExecuteScalar();.如果我在那里得到它,我也会在这里得到它:int correctPassword =(int)sqlCmd.ExecuteScalar();



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();

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();
        }   

    }
}

推荐答案

您好朋友,请尝试以下代码更改:



Hello friend, try the following code changes:

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();





您错过了使用SqlCommand对象添加Connection对象。



You have missed to add the Connection object with the SqlCommand object.


这篇关于ExecuteScalar:尚未初始化Connection属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆