int index = int.Parse(Request.QueryString["index"]);
            try
            {
                connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString.ToString());
                connection.Open();
                command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
                int lines = command.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                Response.Write(err.ToString());
            }
            finally
            {
                connection.Close();
            }
这样写有错误码?为什么老是更新不了?????

解决方案 »

  1.   

    if(!this.ispostpack)
    {
       这里是页面加载的时候所要加载的数据
    }
    加这上个
      

  2.   

    可能代码复制少了
    点击保存按钮的时候运行下面的代码protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Button1.Text == "保存")
            {
                int index = int.Parse(Request.QueryString["index"]);
                try
                {
                    connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString.ToString());
                    connection.Open();
                    command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
                    int lines = command.ExecuteNonQuery();
                }
                catch (Exception err)
                {
                    Response.Write(err.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
        }
      

  3.   

    把你Sql语句打印出去   到查询分析里执行一下   看能不能更新
      

  4.   

    执行:command = new SqlCommand("update [filling] set question='" + this.TextBox1.Text + "', answer='" + this.TextBox2.Text + "', belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " where ID=" + index, connection);
    这一句话的时候,sql语句执行了没有呢?我一直不知道sql语句是什么时候执行的
      

  5.   

    int index = int.Parse(Request.QueryString["index"]);
    这一句在执行时建议楼主判断一下,Request.QueryString["index"]是不是为空呀,会报错的!!!
    建议楼主看一下,index是不是0呀?
      

  6.   

    建议跟踪一下,用sql 执行一下,比较一下,第一:确保数据连接没有问题
    第二:确保可以获取到参数;
    第三:确保sql语句写的正确;应该就没有问题了,sql 建议用string.format()来写,清晰易懂安全。
      

  7.   

    belongto=" + int.Parse(this.DropDownList1.SelectedValue) + "
    加俩个单引号试试  belongto='" + int.Parse(this.DropDownList1.SelectedValue) + "'
      

  8.   

    1.在调试的时候系统有报错么?如果没有报错,应该不是数据连接问题。2.所有参数都能获取么?可以用Response.Write(sql语句)试着在页面上打印出SQL语句,看参数获取的正确性。3.SQL语句中的最后一项,belongto=" + int.Parse(this.DropDownList1.SelectedValue) + " ,试着写为belongto='" + int.Parse(this.DropDownList1.SelectedValue) + "' 或者belongto=" +'"'+ int.Parse(this.DropDownList1.SelectedValue) +'"' 
      

  9.   


    我用一个textbox把sql语句打印出来,发现一个问题,就是this.textbox1.text输出来的不是我最新输入的数据,而是原来的旧的数据,为什么会这样子呢?
      

  10.   

    估计是按照where条件没有满足条件的数据
    1)把每个参数都打出来看看,然后注意以下 belongto,id的类型,如果是char型最好加引号。
    2)id的长度和你传递过来的长度是否相等呢?比如id为char(4)行,传递过来的id是'012'这个时候进行查询需要在最后补一个空格 id = '012 '
      

  11.   


    如果是这样的话,问题就简单多了,你出的问题应该在Page_Load()
    这个地方,把你page_Load()都放在
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {        }
        }
      

  12.   

     if (!Page.IsPostBack) 
            {         } 
    如果你没有加这个的话。那么你更新的数据就还是你第一次绑定的值。所以把你绑定的那些值的方法写到 if (!Page.IsPostBack) 
            { 
    //绑定加载方法
            } 
      

  13.   

    不是很懂楼上两位的意思 我现在基本上知道怎么回事了 按下保存按钮的时候 网页会自动刷新 于是重新调用了page_load函数 执行了里面的代码 于是textbox里面的数据就变回原来数据库中为改变过的数据 怎么让他先保存在刷新页面呢? 能不能这样做?
      

  14.   

    如果是这样的话,在有绑定数据的页面中,不要在Page_Load()中直接绑定数据,而应该这样:protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!Page.IsPostBack) 
            { 
                 bindData(); //绑定数据的过程
            } 
        }因为只要提交了页面,就会刷新页面,就会触发Page_Load(),而导致页面重新绑定,所以更新的数据也就是重新绑定后的数据了。而用IsPostBack就可以判断是否是因为回发导致的页面刷新,这样无论刷新多少次页面也只绑定一次数据了。
      

  15.   


    成功了 明白了
    意思也就是说 当按下页面中的控件导致postback发生的时候 我们不需要重新读取数据库 那么我们可以通过if (!Page.IsPostBack)来区分。谢谢楼上的所有高手!!!