【问题标题】:Gridview Bulk UpdateGridview 批量更新
【发布时间】:2012-05-17 22:05:42
【问题描述】:

从此链接:

asp.net grid view bulk updating all cells

我做了以下事情:

 protected void ButtonUpdate_Click(object sender, EventArgs e)
    {
       // foreach (GridViewRow row in GridView1.Rows)

       // {

        int RowIndex = 0;
        GridViewRow row = (GridViewRow)GridView1.Rows[RowIndex];

            Int32 intresortID = Convert.ToInt32(Request.QueryString["TypeID"]);
            Label dtm = row.FindControl("Label1") as Label;
            Label strRoomType = row.FindControl("strRoomTypeLabel") as Label;
            Label strDescription = row.FindControl("Label3") as Label;
            TextBox Qty = row.FindControl("intQtyTextBox") as TextBox;
            TextBox Price = row.FindControl("curPriceTextBox") as TextBox;
            Label intWSCode = row.FindControl("intWSCodeLabel") as Label;

            string connStr = ConfigurationManager.ConnectionStrings["bedbankstandssConnectionString"].ConnectionString;
            SqlConnection Con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("Update tblAvailable set intqty=@intQty, curprice=@curprice where intresortid=@intresortid and dtm=@dtm and strroomtype=@strroomtype",Con);


            //SqlParameter ddtm= new SqlParameter("@dtm",dtm) ;
            //SqlParameter sstrRoomType = new SqlParameter("@strroomtype", strRoomType);
            //SqlParameter qQty = new SqlParameter("@intQty", Qty);
            //SqlParameter pPrice =new SqlParameter("@curPrice",Price);
            //SqlParameter resortID = new SqlParameter("@intResortID", intresortID);
            cmd.Parameters.AddWithValue("@dtm",dtm.Text.Trim());
            cmd.Parameters.AddWithValue("@strroomtype",strRoomType.Text.Trim());
            cmd.Parameters.AddWithValue("@intQty", Qty.Text.Trim());
            cmd.Parameters.AddWithValue("@curPrice",Price.Text.Trim());
            cmd.Parameters.AddWithValue("@intResortID",intresortID);
            Con.Open();
            cmd.ExecuteNonQuery();
            GridView1.EditIndex = -1;
            DataBind();





       // }
    }

但是只有一行被更新,第一行。

谁能告诉我哪里出错了。

【问题讨论】:

    标签: c# asp.net gridview web-controls gridviewrow


    【解决方案1】:

    你已经注释掉了循环,为什么你想知道只有一行被更新了?

    foreach (GridViewRow row in GridView1.Rows)
    {
        // ...
    }
    

    除此之外,将using-statement 用于您的SqlConnectionSqlCommand 以确保被处置/关闭。

    using(SqlConnection Con = new SqlConnection(connStr))
    {
        using(SqlCommand cmd = new SqlCommand("Update tblAvailable set intqty=@intQty, curprice=@curprice where intresortid=@intresortid and dtm=@dtm and strroomtype=@strroomtype",Con))
        {
            // ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2019-01-29
      • 2011-09-06
      • 2011-04-19
      • 2014-09-18
      相关资源
      最近更新 更多