0

Trying to insert a new record into an MS Access .accdb file. When I run the code below, it appears to work fine. No errors are presented, but nothing updates in the database file either. I have verified that the database is in an accessible location.

selectedNote is an object with the three listed parameters. The only field I'm not including in the query string is the ID field which is autonumber.

string scon = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = |DataDirectory|AMS.accdb";
string str = "INSERT INTO Notes ([ItemID], [Note], [Note Date]) VALUES (?, ?, ?)";

try
{
    using (OleDbConnection con = new OleDbConnection(scon))
    {
        using (OleDbCommand cmd = new OleDbCommand(str, con))
        {
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("ItemID", selectedNote.ItemID);
            cmd.Parameters.AddWithValue("Note", selectedNote.Note);
            cmd.Parameters.AddWithValue("Note Date", selectedNote.NoteDate.ToString("dd-MM-yy"));

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show("Failed due to" + ex.Message);
}

Thanks all, hopefully I can get this hammered out.

EDIT: Curiously, I just found that if I hard-code a path to the .accdb file with a line like the following, it actually does write to that file. So I guess the question becomes why is it not working on the build path where the database is in the same path as the exe.

AppDomain.CurrentDomain.SetData("DataDirectory","C:\temp");

I have tried setting DataDirectory to something like AppDomain.CurrentDomain.BaseDirectory, but this doesn't seem to work either.

5
  • 2
    Set a breakpoint in your code, examine scon and ensure its Data Source is “the database file” you think it is. And stop using AddWithValue. Commented Oct 3, 2020 at 0:57
  • 1
    Is the Access database referenced by your project? If it's part of the build, and you've set it to "Copy Always", your changes would be wiped out each time you debugged it. Commented Oct 3, 2020 at 6:36
  • I do have it referenced, but it is set to "Copy if Newer". I originally did have it as "Copy Always", and hoped that would be the simple fix, but alas. Commented Oct 3, 2020 at 13:36
  • The database file you're looking at and the database file you're making changes to are almost certainly two different files. You haven't given us enough information to guess what is going on, but changing DataDirectory will wreck your data and you must stop making random changes before you permanently destroy your database. Commented Oct 3, 2020 at 16:46
  • I think this is to do with your date if anything. You should always pass the date format as Format("yyyy-MM-dd") as a SQL friendly parameter. Commented Oct 5, 2020 at 20:22

1 Answer 1

2

A date should not be inserted as text, and you do have the DateTime value, thus:

cmd.Parameters.AddWithValue("Note Date", selectedNote.NoteDate);
Sign up to request clarification or add additional context in comments.

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.