Search code examples
c#sqloledbole

SQL - OleDbCommand not updating DB


I am using an OleBdCommand To Insert A record into a Db, but the update never persists.

Here is the Code

    public void InsertCandidate(XElement element, ref OleDbDataAdapter adapter, OleDbConnection sqlConnStr)
    {
        if (sqlConnStr.State == ConnectionState.Broken || sqlConnStr.State == ConnectionState.Closed)
            sqlConnStr.Open();

        try
        {
            string query = "Insert Into Candidate Values(@priKey, @Name, @LName, @Phone, @Add)";
            OleDbCommand InsertCandidate = new OleDbCommand(query, sqlConnStr);
            InsertCandidate.Parameters.AddWithValue("priKey", element.Attribute("CAND_NUM").Value);
            InsertCandidate.Parameters.AddWithValue("Name", element.Attribute("CAND_FNAME").Value);
            InsertCandidate.Parameters.AddWithValue("LName", element.Attribute("CAND_LNAME").Value);
            InsertCandidate.Parameters.AddWithValue("Phone", element.Attribute("CAND_PHONE").Value);
            InsertCandidate.Parameters.AddWithValue("Add", element.Attribute("CAND_ADDRESS").Value);
            InsertCandidate.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("A Error occured whilst trying to execute the command.\n" + ex.Message);
        }
    }

NO exceptions, errors or anomalies are generated!

Any Advice

M


I even tried this.

            //InsertCandidate.ExecuteNonQuery();
            adapter.InsertCommand = InsertCandidate;
            adapter.InsertCommand.ExecuteNonQuery();

*Thanks Every One *

Aiden's Code Worked, but i should have read Tony's Post Better hence i accepted his answer (he said it first)

Kind Regards

And Thanks Once Again!


Solution

  • Have you executed your SQL statement in isolation from your code to make sure it works?

    As you have not posted the table definition it could be because you are not specifying the fields in your insert statement.

    I'm not an OLEDB expert but all the examples I can find on the net use question marks in place of parameters. Try the SQL statement like this:

    string query = "Insert Into Candidate Values(?, ?, ?, ?, ?)";
    

    Update

    Looking at the documentation for the OleDBCommand it will only throw an InvalidOperationException and not an OleDbException in the event of an error. You should add this exception handler and see if that gives you any more information.

    Alternatively change your exception hanndler to catch the generic Exception type to see if you even get an exception.