Click here to Skip to main content
15,991,108 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm using the follwing code for updating values in database which results in an error:

C#
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.
Posted
Updated 16-Mar-12 2:27am
v2
Comments
Bojjaiah 16-Mar-12 8:31am    
post me design code we can see?

What are you trying to do here?

int sn = Convert.ToInt16(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);


Obviously the Control at Controls[0] is not a textbox or anything that can be cast to a texbox (it is a DataControlLinkButton). Therefore the error.
 
Share this answer
 
Comments
Zukiari 16-Mar-12 9:06am    
I want to get the vaue in the cells.
First I have tried with the following code:

string amt = (GridView1.Rows[e.RowIndex].Cells[3].Text).ToString();
string sn = (GridView1.Rows[e.RowIndex].Cells[0].Text).ToString();
con.Open();
//SqlCommand cmd = new SqlCommand("update p_a set frm = '" + Convert.ToDouble(from) + "', up2='"+ Convert.ToDouble(up2) +"', amt='"+ Convert.ToDouble(amt) +"' where sn='"+ Convert.ToInt16(sn) +"'", con);
SqlCommand cmd = new SqlCommand("update p_a set amt='" + Convert.ToDouble(amt) + "' where sn='" + Convert.ToInt16(sn) + "'", con);
//SqlCommand cmd = new SqlCommand("update p_a set amt='"+ amt +"' where sn='"+ sn+"'", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Bindgrid();

But it is also resulting in error:
Input string was not in a correct format.
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.FormatException: Input string was not in a correct format.

Source Error:


Line 58: con.Open();
Line 59: //SqlCommand cmd = new SqlCommand("update p_a set frm = '" + Convert.ToDouble(from) + "', up2='"+ Convert.ToDouble(up2) +"', amt='"+ Convert.ToDouble(amt) +"' where sn='"+ Convert.ToInt16(sn) +"'", con);
Line 60: SqlCommand cmd = new SqlCommand("update p_a set amt='" + Convert.ToDouble(amt) + "' where sn='" + Convert.ToInt16(sn) + "'", con);
Line 61: //SqlCommand cmd = new SqlCommand("update p_a set amt='"+ amt +"' where sn='"+ sn+"'", con);
Line 62: cmd.ExecuteNonQuery();


Thank you.
C#
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();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900