Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a table which has purpose and dateofpayment as it composite key. I am trying
to use my form to delete a row from the table. The code is running but it is not deleting the row from the table
code behind the form

C#
private void btnDelete5_Click(object sender, EventArgs e)
{
            clsStaffsalarypayment person5 = new clsStaffsalarypayment();

            if (MessageBox.Show("Are you really sure of the deletion", "Deletion", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            else
            {
                person5.ID5 = this.cboStaffid5.Text;
                //person.savehistory();a
                person5.removeRegister5();
            }
}

code in class
C#
public void removeRegister5()
{
            //remove the data in memory


            SqlCommand cmd = new SqlCommand();
            string dat = null;
            dat = dateofpayment.Year + "-" + dateofpayment.Month + "-" + dateofpayment.Day;

            string sqlQuery = null;
            sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = '" + purpose + "' and dateofpayment ='" + dat + "'";
            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            cmd.Dispose();
}
Posted
Updated 4-Jun-12 7:33am
v2

Hello

It doesn't delete because the condition returns false. Check the purpose if it's correct then you must check the dat. If all of these are correct then try this:
C#
sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = N'" + purpose + "' and dateofpayment ='" + dat + "'";


Some advises:
1. Do not use conn.Close() after a query because if there be an exception before than conn.Close then the connection never be closed.
Use it in a finally block. or use using

2. The code dosn't need else:
C#
if (MessageBox.Show("Are you really sure of the deletion", "Deletion", MessageBoxButtons.YesNo) == DialogResult.No)
{
    return;
}
//else
//{
person5.ID5 = this.cboStaffid5.Text;
//person.savehistory();a
person5.removeRegister5();
//}
 
Share this answer
 
v2
Comments
VJ Reddy 4-Jun-12 19:48pm    
Good answer. 5!
Shahin Khorshidnia 5-Jun-12 0:45am    
Thank you very much VJ
Abhinav S 4-Jun-12 22:02pm    
Of course. 5.
Shahin Khorshidnia 5-Jun-12 0:45am    
Thank you Abhinav :)
mikeoabban 5-Jun-12 9:48am    
when i run this in sql environment it worked perfect but does not work from my
c# form even if all condition are true
delete from tblstaffsalarypaymentdetails where staffid ='001' and purpose = 'Allowance' and dateofpayment ='2012-06-04'
Put a breakpoint on the line that builds the query string i.e. sqlQuery = "delete from tblstaffsalarypaymentdetails where purpose = '" + purpose + "' and dateofpayment ='" + dat + "'";.
Once the program stops execution, copy this query into sql server management studio and run it (after replacing the parameters).
Check the result - it should most likely help you get to the solution.
 
Share this answer
 
Comments
Shahin Khorshidnia 4-Jun-12 13:50pm    
Good solution +5
Abhinav S 4-Jun-12 22:02pm    
Thank you Shahin.
VJ Reddy 4-Jun-12 19:48pm    
Good suggestion. 5!
Abhinav S 4-Jun-12 22:02pm    
Thank you VJ.
mikeoabban 5-Jun-12 10:34am    
when i run this in sql environment it worked perfect but does not work from my c# form even if all condition are true delete from tblstaffsalarypaymentdetails where staffid ='001' and purpose = 'Allowance' and dateofpayment ='2012-06-04'

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