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.

Josh
  • 115
  • 1
  • 3
  • 17

2 Answers2

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.

Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
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();   

}
Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • I've added this and the code runs with no errors, its just not deleting the data – Josh Feb 24 '16 at 10:31
  • 1
    Try the update query, please and tell me if you get it deleted this time :D – Gnqz Feb 24 '16 at 10:32
  • I have done and still get the same. I added an ELSE clause and also added the changes to the question above if you'd want to take a look – Josh Feb 24 '16 at 10:36
  • 1
    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. – Gnqz Feb 24 '16 at 10:40
  • It goes to the else clause, so the bool isValid is return false – Josh Feb 24 '16 at 10:43
  • 1
    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. – Gnqz Feb 24 '16 at 10:44
  • Okay,the problem seems to go back a fair bit and Im struggling to get my head around it. – Josh Feb 24 '16 at 11:03
  • 1
    You get some unexpected values? – Gnqz Feb 24 '16 at 11:11
  • Although when I use "PayrollNo" to display in the combobox in the previous form, it shows fine. when I send it into the next form it send the string "PayrollNo" instead of the string "2" to be converted to int. – Josh Feb 24 '16 at 11:19
  • Show in your code how you send it to the next form? – Gnqz Feb 24 '16 at 12:34
  • Thanks for all the help, I have asked a new question outlining in more depth exactly where the conversion issue is. http://stackoverflow.com/questions/35602414/c-sharp-converting-string-to-int-from-access-database-and-combobox – Josh Feb 24 '16 at 12:39