0

I'm a novice programmer trying to delete a row from an AccessDatabase using a "DELETE FROM " query. It requires converting a string to an Int which is where the problem occurs

Here's the Code I'm running :

private void BtnDelete_Click(object sender, EventArgs e)
{

    OleDbConnection Conn = new OleDbConnection();
    Conn.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";

    try
    {
         Conn.Open();
            String Query = "DELETE * FROM [Employee] WHERE PayrollNo = @PayrollNo";
            OleDbCommand DoDelete = new OleDbCommand(Query, Conn);
            String PayrollNo = PassPayrollNo;
            int PayRowNum = 0;
            bool isValid = int.TryParse(PassPayrollNo, out PayRowNum);
            if (isValid)
            {
                DoDelete.Parameters.AddWithValue("@PayrollNo", PayRowNum);
                DoDelete.ExecuteNonQuery();
                MessageBox.Show("Success");
                Conn.Close();   
            }
            else
            {
                MessageBox.Show("Could not convert to string");
                Conn.Close();
            }         

}

When I run this I get into the Else clause, so the conversion to string isn't working

What's going wrong? Why? Any help would be greatly appreciated, thanks.

6
  • What is the type of PayrollNo column and what is the type and value of PassPayrollNo? Commented Feb 24, 2016 at 9:46
  • I think PayrollNo is a String but an number in the database Commented Feb 24, 2016 at 9:51
  • You think? Don't you know it? Then you need to parse your string to int if it is valid integer. Commented Feb 24, 2016 at 9:53
  • How would I do that? Commented Feb 24, 2016 at 9:58
  • I have done but the methods I have found seem to give me the same error. Now I could ask another question, or I could ask you? Commented Feb 24, 2016 at 10:06

2 Answers 2

1

Try with passing int type parameter:

int payrowNum = 0;
bool isValid = int.TryParse(PassPayrollNo, out payrowNum);
if(isValid)
{
     string sQuery = "DELETE * FROM [Employee] WHERE PayrollNo = @PayrollNo";
     OleDbCommand DoDelete = new OleDbCommand(sQuery, Conn);
     DoDelete.Parameters.AddWithValue("@PayrollNo", payrowNum);
     int rowsAffected = DoDelete.ExecuteNonQuery();
     if(rowsAffected>0)
     {
         MessageBox.Show("Success");
     }
     Conn.Close();   

}
Sign up to request clarification or add additional context in comments.

11 Comments

I've added this and the code runs with no errors, its just not deleting the data
Try the update query, please and tell me if you get it deleted this time :D
Which clause do you get in? From what I see you still use the string as parameter. As you can see in my anser i pass the integer which was derived from the string. Also consider checking the result from ExecuteNonQuery() method.
So you have an invalid number string. Check it with the debuger or do a debug print with message box and tell us what's in PassPayrollNo.
You get some unexpected values?
|
1

The problem is with your parameter @PayrollNo, you have to make sure the variable PassPayrollNo is the same type as the PayrollNo in your table. If you show the types you are using we can try to help you more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.