Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Sir,
C#
if (e.CommandName == "insertrecord")
        {
            string firstname = ((TextBox)(GridView1.FooterRow.FindControl("txtfirstname")).Text??string.Empty);
            string lastname = ((TextBox)(GridView1.FooterRow.FindControl("txtlastname")).Text??string.Empty);
            string nickname = ((TextBox)(GridView1.FooterRow.FindControl("txtnickname")).Text??string.Empty);
            string salary = ((TextBox)(GridView1.FooterRow.FindControl("salary")).Text??string.Empty);


SIR I FACE SAME ERROR AND SIR ERROR LIKE THIS :-
VB
Error   1   'System.Web.UI.Control' does not contain a definition for 'Text'    C:\Documents and Settings\Harry\My Documents\Visual Studio 2005\example6\Default.aspx.cs    45  92  C:\...\example6\


SIR PLS I CANT EXECUTE THIS CODE PLS SEND ME GOOD SOLUTION FOR THIS..WHICH I CAN EXECUTE VERY WELL

THANK YOU
SIR

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "insertrecord")
       {
           string firstname = (GridView1.FooterRow.FindControl("txtfirstname")).ToString() as TextBox;
           string lastname = (GridView1.FooterRow.FindControl("txtlastname")).ToString() as TextBox;
           string nickname = (GridView1.FooterRow.FindControl("txtnickname")).ToString() as TextBox;
           string salary = (GridView1.FooterRow.FindControl("salary")).ToString() as TextBox;
 
           SqlCommand comm = new SqlCommand();
           comm.CommandText = "insert into tbluser (firstname,lastname,nickname,salary) values(@firstname,@lastname,@nickname,@salary)";
           comm.Connection = conn;
 
           comm.Parameters.AddWithValue("@firstname", firstname.ToString());
           comm.Parameters.AddWithValue("@lastname", lastname.ToString());
           comm.Parameters.AddWithValue("@nickname", nickname.ToString());
           comm.Parameters.AddWithValue("@salary", salary.ToString());
 
           conn.Open();
           comm.ExecuteNonQuery();
           conn.Close();
 
           LoadGridView();
 
       }
When I execute the code I get the error:
Collapse | Copy Code
1 Cannot convert type 'string' to 'System.Web.UI.WebControls.TextBox' via a built-in conversion C:\Documents and Settings\Harry\My Documents\Visual Studio 2005\example6\Default.aspx.cs 45 32 C:\...\example6\
Please help me solve the problem.

Thank you.
Posted
Updated 22-Feb-12 4:10am
v2
Comments
Herman<T>.Instance 22-Feb-12 10:04am    
stay in your original thread with this question
In that thread I'll answer your problem
fjdiewornncalwe 22-Feb-12 11:25am    
Please don't repost questions. This is basically the same question as your last one. If you have updates to add to the question, edit the existing question instead of adding a new one.

1 solution

You would first need to cast to TextBox and then you can get the text of it.
string value = (GridView1.FooterRow.FindControl("txtValue") as TextBox).Text;

Also, you don't need to use ToString if the object already is a string.

Good luck!
 
Share this answer
 
Comments
djharry2 22-Feb-12 13:56pm    
thank you sir I executed the code I get the out put thank you sir..
E.F. Nijboer 23-Feb-12 3:40am    
Great! Happy I could help you out.

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