Click here to Skip to main content
12,825,646 members (35,282 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET4
hi every one
i have a problem in update button.
when i update the record then record removed from the database instead of update???
here is the code of update what's wrong here please guide me ?

thanks in advance
protected void btnupdate_Click(object sender, EventArgs e)

            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Final Project\FinalProject\FinalProject\App_Data\Record.mdf;Integrated Security=True;User Instance=True");

            string query="Update student set firstname='"+bolobj.firstname+"',lastname='"+bolobj.lastname+"',faname='"+bolobj.fname+"',fcellno='"+bolobj.fcellno+"',fnic='"+bolobj.fnic+"',homeno='"+bolobj.homeno+"',gender='"+bolobj.gender+"',days='"+bolobj.days+"',month='"+bolobj.month+"',year='"+bolobj.year+"',classname='"+bolobj.classname+"'where StdId='"+txtID.Text+"'";

            SqlCommand com = new SqlCommand(query, con);
            if (com.ExecuteNonQuery() > 0)


                bolobj.firstname = txtfirstname.Text;
                bolobj.lastname = txtlastname.Text;
                bolobj.fname = txtfaname.Text;
                bolobj.fcellno = txtfcellno.Text;
                bolobj.homeno = txthome.Text;
                bolobj.fnic = txtfnic.Text;
                bolobj.classname = drpclassno.SelectedItem.Text.ToString();
                bolobj.gender = drpgender.SelectedItem.Text.ToString();
                bolobj.days = drpday.SelectedItem.Text.ToString();
                bolobj.month = drpmonth.SelectedItem.Text.ToString();
                bolobj.year = drpyear.SelectedItem.Text.ToString();

                 Label1.Text = " Update Successfully...";

Posted 21-Feb-13 6:39am
Updated 21-Feb-13 8:42am
Taha Akhtar 21-Feb-13 14:27pm
add a breakpoint to string query.. then see the value of your query.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try This one :-

protected void Button1_Click(object sender, EventArgs e)
           Con = new SqlConnection("Your Connection String");

           Cmd = new SqlCommand("update employee set 
           Emp_Name='"+TextBox1.Text+"' where Emp_ID=5",Con);

           if (Cmd.ExecuteNonQuery() > 0)



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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100