Ankita Singh

Ankita Singh

  • NA
  • 159
  • 19k

update data in gridview control

Mar 22 2018 3:56 AM
hello friends... 
 
this is my code
 
protected void update(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnectionstr"].ConnectionString);
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
string qry = "update City_tbl set State=@State,City=@City where id=" + id + "";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@State", SqlDbType.NVarChar).Value = state.Text.Trim();
cmd.Parameters.AddWithValue("@City", SqlDbType.NVarChar).Value = TextBox1.Text.Trim();
// cmd.Parameters.AddWithValue("@State", ((DropDownList)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).SelectedItem.Value.ToString());
//cmd.Parameters.AddWithValue("@City", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindData();
}
 
here state is dropdown control and city is textbox control.. when click update button then value got null value .. plase someone help me.. thankyou

Answers (5)