Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m new in .net i have a problem that in row updating in gridview. text box get only null value..plezzz help me. my code is


C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
int index = GridView1.EditIndex;
        GridViewRow row = GridView1.Rows[index];
        TextBox t1 = row.FindControl("emp_name") as TextBox;
        TextBox t2 = row.FindControl("emp_add") as TextBox;
        TextBox t3 = row.FindControl("emp_mob") as TextBox;
        TextBox t4 = row.FindControl("salary") as TextBox;
        string t5= GridView1.DataKeys[e.RowIndex].Value.ToString();
       string dt = "update employee set emp_name='" + t1 + "',emp_add='" + t2 + "',emp_mob='" + t3+ "',salary='" + t4 + "'where emp_id=" + t5+ "";
        con.Open();
        cmd = new SqlCommand(dt, con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        con.Close();
        bind();
      
    }
Posted
Updated 29-Aug-12 21:06pm
v3
Comments
__TR__ 30-Aug-12 3:10am    
When you ask a question make sure you include the correct tags.
Please go through Code Project Quick Answers FAQ[^]

Take a look at the below article.
Insert, Update, Delete with Gridview ... Simple Way[^]
 
Share this answer
 
You have to use
VB
textBox.Text
Property. I can see you only using textbox and not the text/value inside it..

So it should look like

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
int index = GridView1.EditIndex;
        GridViewRow row = GridView1.Rows[index];
        TextBox t1 = row.FindControl("emp_name") as TextBox;
        TextBox t2 = row.FindControl("emp_add") as TextBox;
        TextBox t3 = row.FindControl("emp_mob") as TextBox;
        TextBox t4 = row.FindControl("salary") as TextBox;
        string t5= GridView1.DataKeys[e.RowIndex].Value.ToString();
       string dt = "update employee set emp_name='" + t1.Text + "',emp_add='" + t2.Text + "',emp_mob='" + t3.Text+ "',salary='" + t4.Text + "'where emp_id=" + t5.Text+ "";
        con.Open();
        cmd = new SqlCommand(dt, con);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        con.Close();
        bind();
      
    }


after getting your comments, it looks, your textbox name is txtemp_name, but your passing emp_name in findcontrol.So its not identifying it.
 
Share this answer
 
v3
Comments
Karl Heinz Hamburg 30-Aug-12 3:13am    
sir framework not take textbox.text
Santhosh Kumar Jayaraman 30-Aug-12 3:14am    
Have you tried t1.Text? Definitely Text property will be there for textboxes
Karl Heinz Hamburg 30-Aug-12 3:18am    
yes i try but it takes null reference exception
Santhosh Kumar Jayaraman 30-Aug-12 3:20am    
it means ur textbox is empty.. value of t1 is null.. Its not able to find any textbox as "empname"
Karl Heinz Hamburg 30-Aug-12 3:27am    
so tell me sir plzz tell me now what i do
Well yes, it will.

Simplfying a bit:

C#
TextBox t1 = row.FindControl("emp_name") as TextBox;
string s = "hello: " + t1;

The second line calls an implicit ToString method on the TextBox object t1, which returns the default object ToString implementation - which is the name of the type of the object: or "System.Web.UI.WebControls.TextBox" rather than the Text content of the textbox itself.

There are three things seriously wrong with your approach:
1) If the control name is not found, FindControl will return null - which will crash your webpage. You must check for this!
2) You should use the TextBox.Text property instead of the whole control.
3) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, or the first person anywhere in teh world who feels like it can destroy your entire database without any difficulty.
 
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