Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried this code for update its not working and its not showing error also while running its just loading but in lable its showing your data is updated.i tried all the exp more that 10 from net.all the samples behaves like the same.plz any one help me.(datakeyname="id,name")

C#
protected void update(object sender, GridViewUpdateEventArgs e)
        {
 
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string name = GridView1.DataKeys[e.RowIndex].Values["Name"].ToString();
            TextBox txtAddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress1");
            TextBox txtAddress2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAddress2");
            TextBox txtCountry = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCountry");
            TextBox txtPincode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPincode");
            con.Open();
            SqlCommand cmd = new SqlCommand("update tbladminreg set Address1='" + txtAddress1.Text + "',Address2='" + txtAddress2.Text + "',Country='" + txtCountry.Text + "',Pincode='" + txtPincode.Text + "' where id=" + id, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label1.Text = name + " Details Updated successfully";
            GridView1.EditIndex = -1;
            gridload();
 
                   }
Posted
Updated 16-May-12 1:18am
v2
Comments
Zoltán Zörgő 16-May-12 7:18am    
And what's the error message?
Build you statement in a string and try to run it in SQL Management Studio. Make it run without errors, than convert it to code. And use parameters, if you dont want SQLi.
hitech_s 16-May-12 7:26am    
can you post the error what you are getting
MAKReddy 16-May-12 7:38am    
can u check ur grid view source side datakeys names is correct or not?
dineshdena 16-May-12 7:41am    
actually im not getting any error.......while i click update its just loading and i have label in the bottom there its showing ur data is updated....but in database its not get updated........

That is a very nasty way to do things, it introduces so many ways that you could get a problem.
First off:
C#
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
Why? Why convert it to a string, in order to convert it back to an int? If the ToString does not return you a numeric value, then the convert will fail and it won't work. If it will return a numeric value, then why are you converting it you a string in the first place?

Secondly: 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. All it would take to destroy you database is a couple of characters in any address field: easy to enter, deliberately or by accident.
C#
SqlCommand cmd = new SqlCommand("update tbladminreg set Address1=@AD1, Address2=@AD2, Country=@CNT, Pincode=@PNC where id=@ID", con);
cmd.Parameters.AddWithValue("@AD1", txtAddress1.Text);
cmd.Parameters.AddWithValue("@AD2", txtAddress2.text);
...


Fix them, see if it works afterwards.
 
Share this answer
 
Comments
Wendelius 16-May-12 16:37pm    
Totally agree!
Try this..

SqlCommand cmd = new SqlCommand("update tbladminreg set Address1='" + txtAddress1.Text.ToString().Trim() + "',Address2='" + txtAddress2.Text.ToString().Trim() + "',Country='" + txtCountry.Text..ToString().Trim() + "',Pincode='" + txtPincode.Text.ToString().Trim() + "' where id=" + id, con);
 
Share this answer
 
C#
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values(0)

try this in ur code behend id place
 
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