1

I am writing because I have a problem with updating my access database with the dataset. i am getting no errors but the database doesn't seem to update. Here is my code:

public partial class Form1 : Form{
private DataSet ds = new DataSet();
string dbconnection = "Provider=Microsoft.JET.OLEDB.4.0;" + @"data source =../../database/Log1.mdb";
string dbcommand = "SELECT * FROM Log";

private void addRow(int action)
    {
        string actionCase = "Uknown action";
        switch (action)
        {
            case 1:
                actionCase = "Nothing";
                break;
            case 2:
                actionCase = "Is";
                break;
            case 3:
                actionCase = "Working";
                break;
            case 4:
                actionCase = "what";
                break;
        }
        DataRow row = ds.Tables["Log"].NewRow();
        row["action"] = actionCase;
        row["actionDateAndTime"] = DateTime.Now.ToString();
        ds.Tables["Log"].Rows.Add(row);
        ds.AcceptChanges();
    }

    private void button_Click(object sender, EventArgs e)
    {
        addRow(1);
        OleDbConnection conn = new OleDbConnection(dbconnection);
        OleDbCommand comm = new OleDbCommand(dbcommand, conn);
        OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
        OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
        adapter.UpdateCommand = builder.GetUpdateCommand();
        adapter.Update(ds, "Log");
    }

The database has 3 columns, ID being a primary key, action and actionDateAndTime, both set to Short Text.

Ppotto
  • 33
  • 6
  • 1
    *"the database doesn't seem to update"* - doesn't seems to update or is not updated? Is database *working* elsewhere? – Sinatr Nov 14 '18 at 15:04
  • 1
    Maybe the output database is copied from project folder to bin every time solution is started. How you check that no rows are in db? – Grzesiek Danowski Nov 14 '18 at 15:05
  • The bin folder is empty and the database is a Microsoft access file that is empty, no lines are being added to it – Ppotto Nov 14 '18 at 15:08
  • 1
    .. and it still empty no matter what you add? How do you check? By restarting the software or by observing file with something else? Typical error is to set "copy to output folder" for mdb-file included into project. – Sinatr Nov 14 '18 at 15:09
  • @Sinatr I can check the file by opening it with access. I can add the fields manually but not through the code – Ppotto Nov 14 '18 at 15:11
  • 1
    Accodring to [this answer](https://stackoverflow.com/a/12915231/1997232) you are missing at least insert command, because you are not updating existing field, but adding a new one. – Sinatr Nov 14 '18 at 15:25
  • 1
    `AcceptChanges()` doesnt do what you think it does. It *clears* all the change flags so that there is nothing to update. Also, if you just have one DataTable, a DataSet is overkill and if you hold one to the DataAdapter you dont have to `GetUpDateCommand` or create a connection every time - a DataAdapter can be very powerful – Ňɏssa Pøngjǣrdenlarp Nov 14 '18 at 15:26
  • @Disaffected1070452 Thank you for your response, the `AcceptChanges()` was the problem, everything works now – Ppotto Nov 14 '18 at 15:35

0 Answers0