none
实现数据更新的问题 RRS feed

  • 问题

  • 实现目标描述:点击“编辑”按钮跳转到另一页面,可进行编辑。
    遇到的问题:编辑内容后,点”更新“按钮,发现内容没有变(还是从数据库查出并赋给编辑控件的值)。

     //这是在前一个页面上点击“编辑”按钮后,跳转到编辑页面后的代码,主要是把需要编辑的内容赋给相应的控件
    SqlConnection conn = null; SqlCommand cmd = null; string shareID = null; protected void Page_Load(object sender, EventArgs e) { //获取页面传过来的主键值 shareID = Request.QueryString["shareID"]; string strSql = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString; conn = new SqlConnection(strSql); //查询该键值的信息 string sql = string.Format("select * from share where shareID='" + int.Parse(shareID) + "'"); cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if(dr.Read()) { try { //从数据库中读取 string shareName = dr["shareName"].ToString(); string shareabout = dr["shareabout"].ToString(); string shareContent = dr["shareContent"].ToString(); DateTime shareAddtime = DateTime.Parse(dr["shareAddtime"].ToString()); string shareHits = dr["shareHits"].ToString(); conn.Close(); //将值赋给控件 txtTitle.Text = shareName; txtInfo.Text = shareabout; FreeTextBox1.Text = shareContent; txtAddtime.Text =shareAddtime.ToString(); txtClicks.Text = shareHits; } catch(System.Exception ex) { Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>"); } finally { conn.Close(); } } }
      //在控件中编辑内容后,更新数据的代码
    protected void BtnUpdate_Click(object sender, EventArgs e) { //获取控件的值 string JCName = txtTitle.Text.Trim(); string JCAbout = txtInfo.Text; string JCContent = FreeTextBox1.Text; DateTime JCAddtime = DateTime.Parse(txtAddtime.Text); int JCHits =int.Parse(txtClicks.Text); //更新数据 try { SqlDataSource ds = new SqlDataSource(); ds.ConnectionString = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString; ds.UpdateCommand = "update share set shareName='" +JCName+ "',shareabout='" + JCAbout + "',shareContent='" + JCContent + "', shareAddtime='" + JCAddtime + "',shareHits='"+JCHits+"' where shareID='"+shareID+"'"; ds.Update(); //更新成功后跳转到管理界面 Response.Redirect("ShareManage.aspx"); } catch(System.Exception ex) { Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>"); } finally { } }
    更新数据的时候,通过断点发现FreeTextBox和TextBox 的值都没有变,还是加载这个页面时赋给控件的值。但是控件中的内容我改了呀?


    高手们帮帮忙!!!万分感激!!!
    2009年10月6日 12:40

答案

  • 以下粗体 更改的 复制粘贴即可

     SqlConnection conn = null;

     

        SqlCommand cmd = null;

     

        string shareID = null;

     

        protected void Page_Load(object sender, EventArgs e)
          {
                    //获取页面传过来的主键值
         
            if(!this.IsPostBack)
            {

             shareID = Request.QueryString["shareID"];
                    string strSql = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString;
            conn = new SqlConnection(strSql);
            //查询该键值的信息
             string sql = string.Format("select * from share where shareID='" + int.Parse(shareID) + "'");
            cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                try
                {

                    //从数据库中读取
                    string shareName = dr["shareName"].ToString();
                    string shareabout = dr["shareabout"].ToString();
                    string shareContent = dr["shareContent"].ToString();
                    DateTime shareAddtime = DateTime.Parse(dr["shareAddtime"].ToString());
                    string shareHits = dr["shareHits"].ToString();
                    conn.Close();
                    //将值赋给控件
                    txtTitle.Text = shareName;
                    txtInfo.Text = shareabout;
                    FreeTextBox1.Text = shareContent;
                    txtAddtime.Text = shareAddtime.ToString();
                    txtClicks.Text = shareHits;
                }
                catch (System.Exception ex)
                {
                    Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>");
                }
                finally
                {
                    conn.Close();
                }
            }
           }
        }
      //在控件中编辑内容后,更新数据的代码  
         protected void BtnUpdate_Click(object sender, EventArgs e)
       {
           //获取控件的值
           string JCName = txtTitle.Text.Trim();

     

            string JCAbout = txtInfo.Text;

     

            string JCContent = FreeTextBox1.Text;

     

            DateTime JCAddtime = DateTime.Parse(txtAddtime.Text);

     

            int JCHits =int.Parse(txtClicks.Text);

     

            //更新数据

     

             try
            {
                shareID = Request.QueryString["shareID"];
                string strSql = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString;
                conn = new SqlConnection(strSql);
                string UpdateCommand = "update share set shareName='" + JCName + "',shareabout='" + JCAbout + "',shareContent='" + JCContent + "', shareAddtime='" + JCAddtime + "',shareHits='" + JCHits + "' where shareID='" + shareID + "'";
                cmd = new SqlCommand(UpdateCommand, conn);
                conn.Open();
                int i= cmd.ExecuteNonQuery();
                 //更新成功后跳转到管理界面
                if(i>0)
                Response.Redirect("ShareManage.aspx");
            }
            catch(System.Exception ex)
            {          
                Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>");
            }
            finally
           {
               conn.Close();
            }      

     

        }


     
        }

    • 已标记为答案 斌斌NO1 2009年10月6日 13:41
    2009年10月6日 13:13

全部回复

  • 以下粗体 更改的 复制粘贴即可

     SqlConnection conn = null;

     

        SqlCommand cmd = null;

     

        string shareID = null;

     

        protected void Page_Load(object sender, EventArgs e)
          {
                    //获取页面传过来的主键值
         
            if(!this.IsPostBack)
            {

             shareID = Request.QueryString["shareID"];
                    string strSql = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString;
            conn = new SqlConnection(strSql);
            //查询该键值的信息
             string sql = string.Format("select * from share where shareID='" + int.Parse(shareID) + "'");
            cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                try
                {

                    //从数据库中读取
                    string shareName = dr["shareName"].ToString();
                    string shareabout = dr["shareabout"].ToString();
                    string shareContent = dr["shareContent"].ToString();
                    DateTime shareAddtime = DateTime.Parse(dr["shareAddtime"].ToString());
                    string shareHits = dr["shareHits"].ToString();
                    conn.Close();
                    //将值赋给控件
                    txtTitle.Text = shareName;
                    txtInfo.Text = shareabout;
                    FreeTextBox1.Text = shareContent;
                    txtAddtime.Text = shareAddtime.ToString();
                    txtClicks.Text = shareHits;
                }
                catch (System.Exception ex)
                {
                    Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>");
                }
                finally
                {
                    conn.Close();
                }
            }
           }
        }
      //在控件中编辑内容后,更新数据的代码  
         protected void BtnUpdate_Click(object sender, EventArgs e)
       {
           //获取控件的值
           string JCName = txtTitle.Text.Trim();

     

            string JCAbout = txtInfo.Text;

     

            string JCContent = FreeTextBox1.Text;

     

            DateTime JCAddtime = DateTime.Parse(txtAddtime.Text);

     

            int JCHits =int.Parse(txtClicks.Text);

     

            //更新数据

     

             try
            {
                shareID = Request.QueryString["shareID"];
                string strSql = ConfigurationManager.ConnectionStrings["WebConn"].ConnectionString;
                conn = new SqlConnection(strSql);
                string UpdateCommand = "update share set shareName='" + JCName + "',shareabout='" + JCAbout + "',shareContent='" + JCContent + "', shareAddtime='" + JCAddtime + "',shareHits='" + JCHits + "' where shareID='" + shareID + "'";
                cmd = new SqlCommand(UpdateCommand, conn);
                conn.Open();
                int i= cmd.ExecuteNonQuery();
                 //更新成功后跳转到管理界面
                if(i>0)
                Response.Redirect("ShareManage.aspx");
            }
            catch(System.Exception ex)
            {          
                Response.Write("<Script Language=JavaScript>alert('" + ex.Message.ToString() + "')</Script>");
            }
            finally
           {
               conn.Close();
            }      

     

        }


     
        }

    • 已标记为答案 斌斌NO1 2009年10月6日 13:41
    2009年10月6日 13:13
  •  //将值赋给控件

    放在if(!Page.IsPostBack)
    {



    }
    里面


    【孟子E章】
    2009年10月6日 13:16
    版主
  • 谢谢老师!!!
    2009年10月6日 13:41