GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  Ado.NET
Go To First  |  Previous Question  |  Next Question 
 Ado.NET  |  Question 28 of 31    Print  
How to update a column in DataGrid using C#
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();



  
Total Answers and Comments: 3 Last Update: June 01, 2009     Asked by: pen2satya 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 01, 2007 09:58:44   #1  
santosh.net.india        

RE: How to update a column in DataGrid using C#
Did u not set the data key property of datagrid pl set key in data key feild
 
Is this answer useful? Yes | No
July 10, 2008 09:16:19   #2  
sureshnaspuri Member Since: July 2008   Contribution: 3    

RE: How to update a column in DataGrid using C#
After seeing ur code i understood that u haven't call the bind method.

gridview1.EditItemIndex -1;
after this u call the bind method .

gridview1.databind();//in this place u call the grid bind method n try it once.


 
Is this answer useful? Yes | No
June 01, 2009 06:02:32   #3  
ponss Member Since: June 2009   Contribution: 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();

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape