另一个SqlParamenterCollection已包含SqlParamenter
protected void btnUpdate_Click(object sender, EventArgs e)
{
string sql = " update [UserInfo] set UserName =@UserName ,Sex=@Sex,Age=@Age,Descripition=@Descripition,Position=@Position";
if (GetMD5(txtPassword.Text.Trim()).Length > 0)
{
sql += ",Pwd=@Pwd";
}
sql += " where [ID]=@ID";
SqlParameter[] parameters = new SqlParameter[8];
SqlParameter name = new SqlParameter("@UserName", SqlDbType.VarChar);
name.Value = this.txtName.Text;
parameters[0] = name;
SqlParameter sex = new SqlParameter("@Sex", SqlDbType.Int);
sex.Value = int.Parse(this.rdlSex.SelectedValue);
parameters[1] = sex;
SqlParameter age = new SqlParameter("@Age", SqlDbType.Int);
sex.Value = int.Parse(this.txtAge.Text.Trim());
parameters[2] = sex;
SqlParameter description = new SqlParameter("@Descripition", SqlDbType.VarChar);
description.Value = this.ddlDes.Text;
parameters[3] = description;
SqlParameter position = new SqlParameter("@Position", SqlDbType.VarChar);
description.Value = this.ddlDes.Text;
parameters[4] = description;
SqlParameter password = new SqlParameter("@Pwd", SqlDbType.VarChar);
password.Value = GetMD5(this.txtPassword.Text.Trim());
parameters[5] = password;
SqlParameter id = new SqlParameter("ID", SqlDbType.VarChar);
id.Value =this.hfdUserID.Value;
parameters[6] = id;
try
{
bool result = dbHelper.ExecuteNonQuery(sql, parameters);
Response.Write("<script type='text/javascript'> alert('更新成功');</script>");
}
catch (Exception ex)
{
Response.Write("<script type='text/javascript'> alert('更新失败,失败原因:" + ex.Message + "');</script>");
}
}
private DataRow GetUser(string id)
{
string sql = "select * from [UserInfo] where ID=@ID";
SqlParameter paramID = new SqlParameter("@ID", SqlDbType.VarChar);
paramID.Value = this.hfdUserID.Value;
DataTable dtData;
try
{
dtData = dbHelper.Select(sql, new SqlParameter[] { paramID });
}
catch (Exception ex)
{
throw ex;
}
if (dtData.Rows.Count > 0)
{
return dtData.Rows[0];
}
else
{
return null;
}
}
各位大神求教。
------解决方案--------------------
太粗心啦
protected void btnUpdate_Click(object sender, EventArgs e)
{
string sql = " update [UserInfo] set UserName =@UserName ,Sex=@Sex,Age=@Age,Descripition=@Descripition,Position=@Position";
if (GetMD5(txtPassword.Text.Trim()).Length > 0)
{
sql += ",Pwd=@Pwd";
}
sql += " where [ID]=@ID";
SqlParameter[] parameters = new Sql