这个是后台代码。ID是另一个页面传递过来的,不知道为什么当我点击修改的时候显示是修改成功的,实际上数据库还没被修改,悲剧啊
  protected void Page_Load(object sender, EventArgs e)
    {
        uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
        string id = Request.Params["id"];
        this.hezuoid.Text = id;
        
        SqlConnection con = new SqlConnection(connectionString);
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                this.hezuoname.Text = dr["name"].ToString();
                this.hezuoid.Text = dr["id"].ToString();            }
            dr.Dispose();
            cmd.Dispose();        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }    }
    protected void uphezuo_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.Append(" update cooperation set  ");
        sqlStr.Append(" name='" + hezuoname.Text + "'");
        sqlStr.Append(" where id='" + hezuoid.Text + "'");
        SqlCommand cmd = new SqlCommand(sqlStr.ToString(), con);
        int i = cmd.ExecuteNonQuery();
        if (i == 0)
        {
            lesc.Text = "<script>alert('修改失败,没做任何修改!')</script>";
        }
        else
        {
            lesc.Text = "<script>alert('修改成功!')</script>";
            Response.AddHeader("refresh", "0");
        }
        
                  }

解决方案 »

  1.   

    在SqlCommand cmd = new SqlCommand(sqlStr.ToString(), con);
    设置一断点 
    看一下sqlStr是多少,注意参数的值
      

  2.   

    ps:你的update操作后没有关闭SqlConnection 
      

  3.   


    不添加ispostback会影响页面的显示但是不会影响数据库内的update操作我怀疑是他sql语句内参数问题
      

  4.   

    if(!ispostback)
    {}
    这个 方法怎么用,忘记了。貌似要用这个方法才能修改面部过也谢谢q107770540的确忘记关闭了,呵呵
      

  5.   


     protected void Page_Load(object sender, EventArgs e)
        {
     if (!Page.IsPostBack)  //加上此句 后,page_load中只有一个select操作,应该不会影响你接下来的update
            {
            uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
            string id = Request.Params["id"];
            this.hezuoid.Text = id;
            
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.hezuoname.Text = dr["name"].ToString();
                    this.hezuoid.Text = dr["id"].ToString();            }
                dr.Dispose();
                cmd.Dispose();        }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
    }
        }
      

  6.   

    if(!IsPostBack)
    {
    id = Request.Params["id"]==null?"":Request.Params["id"].ToString();
    this.hezuoid.Text = id;
    }
    public string id
    {
    get{return ViewState["id"].ToString();}
    set{ViewState["id"]=value;}
    }
    using(SqlConnection con = new SqlConnection(connectionString))
    {}
      

  7.   

    this.hezuoname.Text 永远都是第一次从数据读出来的值,所以你改成功了,还是那个值。加上一个!IsPostBackprotected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack==false)
            {
            uphezuo.Attributes.Add("onclick", @"javascript:if(confirm('你是否真的要修改吗?')){return true;}else{return false;}");
            string id = Request.Params["id"];
            this.hezuoid.Text = id;
            
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from cooperation where id='"+hezuoid.Text+"'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.hezuoname.Text = dr["name"].ToString();
                    this.hezuoid.Text = dr["id"].ToString();            }
                dr.Dispose();
                cmd.Dispose();        }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
          }
        }
      

  8.   

    谢谢
    果然加了if(!IsPostBack)
    方法后可以修改了
      

  9.   

    另外你的update方法,con要关闭