I'm using the follwing code for updating values in database which results in an error:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int sn = Convert.ToInt16(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
Double amt = Convert.ToDouble(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
con.Open();
SqlCommand cmd = new SqlCommand("update p_a set amt='"+ amt +"' where sn='"+ sn+"'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Bindgrid();
}
The error is :
Server Error in '/DIS' Application.
--------------------------------------------------------------------------------
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Source Error:
Line 44: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
Line 45: {
Line 46: int sn = Convert.ToInt16(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
Line 47: Double amt = Convert.ToDouble(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
Line 48: //string from = (GridView1.Rows[e.RowIndex].Cells[0].Text).ToString();
Stack Trace:
[InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.]
prod_anc.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\Users\ppdtvtt-jismiya\Documents\Visual Studio 2008\WebSites\DIS\prod_anc.aspx.cs:46
System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +133
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +720
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4963; ASP.NET Version:2.0.50727.4971
What is the solution for this? please anybody help me...
Thank you.