Mr.Lio
Mr.Lio

Reputation: 21

Delete a SQL table record using C# & SQL WHERE clause

I want to delete a SQL table record using C#. I already tried the below mentioned code snippet but it didn't work. However the message is showing 'Data Record Deleted!' as it should it be. I can't find an issue in the statement also.

private void delemp_Click(object sender, EventArgs e)
{
    try
    {
        if(MessageBox.Show("do you really want to delete this","Deleting ", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
        {
            EMP.DELETEMP(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            MessageBox.Show("deleting Seccussfully","Deleting ");
            this.dataGridView1.DataSource = EMP.GET_ALL_EMP();
        }
        else
        {
            MessageBox.Show("Deleting Canceled ");
        }
    }
    catch (Exception)
    {
        MessageBox.Show("ERROR");
    }            
}

and this is the class

public void DELETEMP(string ID)
{
        DAL.dataaccesslayer DAL = new DAL.dataaccesslayer();
        DAL.open();
        SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@ID", SqlDbType.NVarChar);
        param[0].Value = ID;
        DAL.executcommand("DELETEMP", param);
        DAL.close();
}

Upvotes: 0

Views: 342

Answers (1)

robbpriestley
robbpriestley

Reputation: 3300

EMP.DELETEMP() doesn't return a result of any kind so it will be impossible for you to know whether the record was actually deleted or not. As long as EMP.DELETEMP() doesn't throw an exception, which it might not, your code will always display the "deleting Seccussfully" message.

So, most likely EMP.DELETEMP() isn't actually deleting anything. To fix this, you are going to need to figure out why. It looks like there is maybe a stored procedure in the database called DELETEMP that takes an ID (integer?) parameter. Have you tested this stored procedure?

Also, we are assuming that DAL works as intended. I do not recognize this. It is possibly not fully legitimate because the method you are calling is DAL.executcommand() which isn't even spelled correctly.

Upvotes: 1

Related Questions