I am trying to update record in grid view using following commmand.But it does not Update the record.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("label8");
TextBox txtq = (TextBox)row.FindControl("TextBox7");
GridView1.EditIndex = -1;
cn.Open();
string sql = "UPDATE Q_Master SET Que = '" + txtq.Text + " ' WHERE Q_id='" + lbl.Text + "' ";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.ExecuteNonQuery();
cn.Close();
LoadBind();
}
LoadBind()
{
string sql = "SELECT * FROM Q_Master";
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "Q_Master");
GridView1.DataSource = ds;
GridView1.DataBind();
}
Loading
PankaJ DPosted Jan 4, 2011, 7:06 AM
To Krishna Garad Sir.....
solved....thanx
Krishna GaradPosted Jan 4, 2011, 6:57 AM
if(!IsPostBack)
{
// Bind your gris here. This is the general cause values not get change becoz of firstly
//the page_load event will fire befire any event thats why it is taking old values.
}
PankaJ DPosted Jan 4, 2011, 6:47 AM
Krishna GaradPosted Jan 4, 2011, 4:40 AM
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtq= (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
Lable lbl=(Lable)GridView1.Rows[e.RowIndex].FindControl("Lable8");
cn.Open();
string sql = "UPDATE Q_Master SET Que = '" + txtq.Text + " ' WHERE Q_id='" + lbl.Text + "' ";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.ExecuteNonQuery();
cn.Close();
Gridview1.EditIndex=-1;
LoadBind();
}
LoadBind()
{
string sql = "SELECT * FROM Q_Master";
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "Q_Master");
GridView1.DataSource = ds;
GridView1.DataBind();
}