Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
but the error displays like tat...
C++
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'

tis s t code

XML
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                        CellPadding="3" CellSpacing="2" OnPageIndexChanging="GridView1_PageIndexChanging"
                        OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" PageSize="3">
                        <Columns>
                            <asp:CommandField ShowEditButton="True" />
                            <asp:BoundField DataField="name" HeaderText="Name" />
                        </Columns>
                    </asp:GridView>


C++
SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["rajarani connection"]);
           con1.Open();
           SqlDataAdapter adp1 = new SqlDataAdapter("update registerform set fullname='" + ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text + "' where registerform.id='"+GridView2.Rows[e.RowIndex].Cells[0].Text+"' ", con1);
           DataSet ds1 = new DataSet();
           adp1.Fill(ds1);
           con1.Close();
           GridView2.EditIndex = -1;
           bind();



plz..help me ..
Posted
Updated 5-Aug-10 20:54pm
v3
Comments
koool.kabeer 5-Aug-10 8:25am    
you are purely casting the type of "System.Web.UI.WebControls.DataControlLinkButton"
to the type of "System.Web.UI.WebControls.TextBox"..
which you cant do...
may be you are giving index of Control incorrectly..
check either the index of Cell in "..Cell[1]..."
or the index of Control in "...Cell[1].Controls[0]..."...

Can you try,
((LinkButton)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text

I hope this will work.
 
Share this answer
 
Comments
mehalanandu 5-Aug-10 8:44am    
thanks for gven ... i tried tis code error s not displayd but it s not updat t value
It simply looks like your casting is incorrect
(TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text


Try to test the object first before casting:
if(typeof(TextBox).IsAssignableFrom(GridView2.Rows[e.RowIndex].Cells[1].Controls[0].GetType()))
{
  // ...cast value ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
}
;


Good luck!
 
Share this answer
 
Comments
mehalanandu 5-Aug-10 8:47am    
thanks for gven ... i tried tis code, error s not displayd but it s not updat t value
E.F. Nijboer 5-Aug-10 9:34am    
Maybe your reference is simply incorrect and therefore you get the LinkButton instead of the TextBox you are looking for. Try another Cells[#] value or look in the debugger if the Controls[] array has more controls than one. If there are more you can loop through all of them and find the control you are looking for.
I belive the following line is creating the error

((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text

It seems that in Cells[1] you have used a LinkButton rather than a TextBox, So when you are type casting that LinkButton to TextBox the runtime is throwing the error.

If not clear, please provide the markup of the GridView control from .aspx file.
 
Share this answer
 
Comments
mehalanandu 6-Aug-10 2:21am    
thanks for ur idea...i need the perfect code to updat the gridview ..plz help me
senguptaamlan 6-Aug-10 2:28am    
plz provide the gridview markup from the .aspx file.
mehalanandu 6-Aug-10 2:52am    
Source code:
<asp:gridview id="GridView1" runat="server" allowpaging="True" autogeneratecolumns="False"
="" cellpadding="3" cellspacing="2" onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" pagesize="3">
<columns>
<asp:commandfield showeditbutton="True">
<asp:boundfield datafield="name" headertext="Name">

mehalanandu 6-Aug-10 2:54am    
I attach the code ...
refer tat..plz
senguptaamlan 6-Aug-10 3:07am    
Replace the type casting with the following line of code
GridView2.Rows[e.RowIndex].Cells[1].Controls[0].Text

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