我正在尝试让我的更新功能正常工作。我已经包含了txtPassword和txtConfirmPassword文本框。这两个文本框必须在更新之前匹配,但是我收到此错误:
ExecuteNonQuery: CommandText property has not been initialized
我怎样才能让它发挥作用?这是我的代码:
protected void btnUpdateAccount_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (txtPassword.Text == "")
{
cmd.CommandText = "UPDATE Users SET EmailAddress=@EmailAddress, FirstName=@FirstName, " +
"LastName=@LastName, Street=@Street, Municipality=@Municipality, City=@City, ZipCode=@ZipCode, ContactNo=@ContactNo, Image=@Image " +
"WHERE UserID=@UserID";
}
else
{
if (txtConfirmPassword != txtPassword)
{
pnlInconsistent.Visible = true;
}
else
{
cmd.CommandText = "UPDATE Users SET Password=@Password, EmailAddress=@EmailAddress, FirstName=@FirstName, " +
"LastName=@LastName, Street=@Street, Municipality=@Municipality, City=@City, ZipCode=@ZipCode, ContactNo=@ContactNo, Image=@Image " +
"WHERE UserID=@UserID";
}
cmd.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = Helper.CreateSHAHash(txtPassword.Text);
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFN.Text;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLN.Text;
//cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text;
cmd.Parameters.Add("@Street", SqlDbType.NVarChar).Value = txtStreet.Text;
cmd.Parameters.Add("@Municipality", SqlDbType.NVarChar).Value = txtMunicipality.Text;
cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity.Text;
cmd.Parameters.Add("@ZipCode", SqlDbType.NVarChar).Value = txtZipCode.Text;
cmd.Parameters.Add("@ContactNo", SqlDbType.NVarChar).Value = txtContact.Text;
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"].ToString();
if (fuImage.HasFile)
{
cmd.Parameters.Add("@Image", SqlDbType.Text).Value = "~/images/" + fuImage.FileName;
fuImage.SaveAs(Server.MapPath("~/images/" + fuImage.FileName));
}
else
{
cmd.Parameters.Add("@Image", SqlDbType.Text).Value = imgAvatar.ImageUrl;
}
if (txtPassword.Text != "")
{
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value =
Helper.CreateSHAHash(txtPassword.Text);
}
cmd.ExecuteNonQuery();
con.Close();
GetProfileInfo();
pnlUpdated.Visible = true;
Helper.AddLog(Session["userid"].ToString(), "Update", "Updated Profile Information");
GetProfileInfo();
}
}
答案 0 :(得分:0)
看起来,如果此if (txtConfirmPassword != txtPassword)
为真,则更新查询将始终执行,commandText
参数将不会设置,因为cmd.ExecuteNonQuery();
不是else
的一部分1}}语句,它可能应该是。如果密码和txtPassword与txtConfirmPassword不匹配,我猜你想要停止执行查询,但我没有看到这种情况发生。
此if-check似乎也是多余的,因为只有在txtPassword不为空时才会执行:
if (txtPassword.Text != "")
{
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value =
Helper.CreateSHAHash(txtPassword.Text);
}
答案 1 :(得分:0)
对于cmd.ExecuteNonQuery();
未初始化txtConfirmPassword != txtPassword
的情况,您的代码也会执行CommandText
行。
在return;
之后为此案例添加pnlInconsistent.Visible = true;
:
if (txtConfirmPassword != txtPassword)
{
pnlInconsistent.Visible = true;
return;
}
或将所有cmd.Parameters.Add()
和cmd.ExecuteNonQuery();
包装在嵌套else
中:
else
{
cmd.CommandText = "UPDATE Users SET Password=@Password, EmailAddress=@EmailAddress, FirstName=@FirstName, " + "LastName=@LastName, Street=@Street, Municipality=@Municipality, City=@City, ZipCode=@ZipCode, ContactNo=@ContactNo, Image=@Image " + "WHERE UserID=@UserID";
cmd.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = Helper.CreateSHAHash(txtPassword.Text);
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFN.Text;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLN.Text;
//cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text;
cmd.Parameters.Add("@Street", SqlDbType.NVarChar).Value = txtStreet.Text;
cmd.Parameters.Add("@Municipality", SqlDbType.NVarChar).Value = txtMunicipality.Text;
cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity.Text;
cmd.Parameters.Add("@ZipCode", SqlDbType.NVarChar).Value = txtZipCode.Text;
cmd.Parameters.Add("@ContactNo", SqlDbType.NVarChar).Value = txtContact.Text;
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"].ToString();
if (fuImage.HasFile)
{
cmd.Parameters.Add("@Image", SqlDbType.Text).Value = "~/images/" + fuImage.FileName;
fuImage.SaveAs(Server.MapPath("~/images/" + fuImage.FileName));
}
else
{
cmd.Parameters.Add("@Image", SqlDbType.Text).Value = imgAvatar.ImageUrl;
}
if (txtPassword.Text != "")
{
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value =
Helper.CreateSHAHash(txtPassword.Text);
}
cmd.ExecuteNonQuery();
con.Close();
GetProfileInfo();
pnlUpdated.Visible = true;
Helper.AddLog(Session["userid"].ToString(), "Update", "Updated Profile Information");
GetProfileInfo();
}