From this link:
asp.net grid view bulk updating all cells
I did the following:
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();
// }
}
But only one row gets updated, the first one.
Would somebody be able to tell me where I went wrong please.
You have commented out the loop, so why are you wondering that only one row is updated?
foreach (GridViewRow row in GridView1.Rows)
{
// ...
}
Apart from that, use the using-statement
for your SqlConnection
and SqlCommand
to ensure that is gets disposed/closed.
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))
{
// ...
}
}