Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi how to delete records from sql table when i show values in textbox and when i hit delete button record should delete from sql table..can any one help me how to give query for that
??
Posted
Comments
uspatel 5-Mar-12 4:10am    
have you tried any thing, share code....

 
Share this answer
 
DevGuru Quick reference.

Beginners hate this, but I must repeat this mantra:
Do a Google search first.
 
Share this answer
 
private void Delete ( )
    {
        try
        {
            if (TxtEmpcode.Text == "")
            {
                MessageBox.Show( "Please Enter the valid Employee Code", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
                TxtEmpcode.Focus ( );


                TxtEmpcode.Text = "";
                TxtEmpName.Text = "";
                TxtQual.Text = "";
                TxtCom.Text = "";
                TxtAdd.Text = "";
            }
            else
            {
//before delete validate the requested id is there or not

                objconnection = new SqlConnection ( WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString );
                SqlCommand objcommand = new SqlCommand ( );
                objcommand.Connection = objconnection;
                objcommand.CommandType = CommandType.Text;
                objcommand.CommandText = "Select Count(*) from Employee where EmpCode=" + TxtEmpcode.Text.Trim ( ) + "";
                objconnection.Open ( );
                int val = int.Parse ( objcommand.ExecuteScalar ( ).ToString ( ) );

                if (val > 0)
                {
//delete record here
                    objconnection = new SqlConnection ( WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString );
                    objcommand = new SqlCommand ( );
                    objcommand.Connection = objconnection;
                    objcommand.CommandType = CommandType.Text;
                    objcommand.CommandText = "Delete from Employee where EmpCode=" + TxtEmpcode.Text.Trim ( ) + "";
                    objconnection.Open ( );
                    objcommand.ExecuteNonQuery ( );
                    objconnection.Close ( );

                    MessageBox.Show ( " Employee record is successfully Deleted", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
                    TxtEmpcode.Focus ( );
                    TxtEmpcode.Text = "";
                    TxtEmpName.Text = "";
                    TxtQual.Text = "";
                    TxtCom.Text = "";
                    TxtAdd.Text = "";
                }
                else
                {
                    MessageBox.Show ( "Please Enter the valid Employee Code", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
                    MessageBox.Show ( "Employee record is successfully deleted" + TxtEmpcode.Text, "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );

                    TxtEmpcode.Focus ( );
                    TxtEmpcode.Text = "";
                    TxtEmpName.Text = "";
                    TxtQual.Text = "";
                    TxtCom.Text = "";
                    TxtAdd.Text = "";

                }
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show ( "error occur", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Error );

        }
        finally
        {
          objconnection.Close();
        }
    }
 
Share this answer
 
v2
 
Share this answer
 
Please read this, may this help you...
http://www.w3schools.com/sql/sql_delete.asp
 
Share this answer
 
v3
Comments
Anuja Pawar Indore 5-Mar-12 4:22am    
Added link tag
amolpatil2243 5-Mar-12 4:23am    
thanks, but i tried to give the link tag, but not accepted by system?? why

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