| |
GeekInterview.com > Interview Questions > Microsoft > Ado.NET
| Print | |
Question: How to update a column in DataGrid using C#
Answer: I am getting InvalidCastException as (Specified cast is not valid) while updating 2nd column in a datagrid? Id,firstname,lastname are the three columns of my datagrid respectively. I wanted to edit the second column(lastname) and update it. I did the following code in DataGrid's updatecommand(),but failed to update ! Int varid=(int)DataGrid1.DataKeys[e.Item.ItemIndex]; TextBox lnm=(TextBox)e.Item.Cells[2].Controls[0]; string str=lnm.Text ; SqlCommand cmd=new SqlCommand("update customer set lastname='" + str + "' where id=" + varid + "",con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex=-1; DataGrid1.DataBind(); |
| June 06, 2009 06:02:32 |
#3 |
| ponss |
Member Since: June 2009 Total Comments: 3 |
RE: How to update a column in DataGrid using C# |
int varid = (int)DataGrid1.DataKeys[e.Item.ItemIndex]; TextBox lnm = (TextBox)e.Item.Cells[2].Controls[0]; string str = lnm.Text.ToString(); SqlCommand cmd = new SqlCommand("update customer set lastname='" + str + "' where id=" + varid, con); cmd.ExecuteNonQuery(); DataGrid1.EditItemIndex = -1; DataGrid1.DataBind();
|
| |
Back To Question | |