谁能帮我解决这个错误。它说,
变量名称“@UserID”已被声明。变量名称在查询批处理或存储过程中必须是唯一的。关键字“WHERE”附近的语法不正确。
错误出现在cmd.ExecuteNonquery();
这是我的代码:
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, Address=@Address, ContactNo=@ContactNo, Image=@Image, " +
"WHERE UserID=@UserID";
}
else
{
cmd.CommandText = "UPDATE Users SET Password=@Password, EmailAddress=@EmailAddress, FirstName=@FirstName, " +
"LastName=@LastName, Address=@Address, 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("@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;
}
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"].ToString();
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();
}
随时发表评论以获取更多详细信息。