1

谁能帮我解决这个错误。它说,

变量名称“@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();
    }

随时发表评论以获取更多详细信息。

4

3 回答 3

2

正如 JW 在他的评论中所说,您不需要在WHERE参与 sql 查询之前使用逗号。

正确使用这些;

    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";
    }

您还在查询中添加@UserID了两次参数。在你SqlCommand和你添加它之后,它也作为一个参数。你不能那样做。您需要删除其中之一。你可以检查他们

    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;
  here --> cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["userid"].ToString();

    else
    {
        cmd.Parameters.Add("@Image", SqlDbType.Text).Value = imgAvatar.ImageUrl;
    }
  here --> cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"].ToString();
于 2013-06-24T06:56:43.050 回答
1

@UserID您多次添加参数

于 2013-06-24T06:56:28.563 回答
0

1)删除Image=@Image查询中的逗号。

2)cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Session["UserID"].ToString();写了两次,去掉其中一个。

于 2013-06-24T07:12:22.763 回答