Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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);
            con.Open();
            if (com.ExecuteNonQuery() > 0)

                con.Close();

               {
                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
Updated 21-Feb-13 7:42am
v2
Comments
Taha Akhtar 21-Feb-13 14:27pm    
add a breakpoint to string query.. then see the value of your query.

1 solution

Try This one :-

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

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

          
           if (Cmd.ExecuteNonQuery() > 0)
           {

                   Con.Close();
                   Response.Write("Done..");
           }


          
       }
 
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