Click here to Skip to main content
11,575,595 members (64,644 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
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);
            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 21-Feb-13 5:39am
Edited 21-Feb-13 7:42am
v2
Comments
Taha Akhtar at 21-Feb-13 14:27pm
   
add a breakpoint to string query.. then see the value of your query.

1 solution

Rate this: bad
good
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");
 
           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..");
           }
 

          
       }
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2015
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