Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
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.
Posted 16 Mar '12 - 2:26
Zukiari1.1K
Edited 16 Mar '12 - 2:27

Comments
Bojjaiah - 16 Mar '12 - 8:31
post me design code we can see?

2 solutions

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.
  Permalink  
Comments
Zukiari - 16 Mar '12 - 9:06
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.
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();
    }
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,028
1 OriginalGriff 6,010
2 CPallini 3,432
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid