我在 Visual Studio 2012 上的代码有问题。它应该更新可以在我的 SQL Server Management Studio 上找到的记录。除此更新外,一切都与数据库一致。请看一下我的代码:
protected void btnUpdate_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
{
cmd.CommandText = "UPDATE Users SET Password=@Password, EmailAddress=@EmailAddress, FirstName=@FirstName, " +
"LastName=@LastName, Address=@Address, 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;
}
Helper.AddLog(Session["UserID"].ToString(), "Update", "Updated a User");
Response.Redirect("Default.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}