Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Update MDB file?

How to Update MDB file?

Scheduled Pinned Locked Moved C#
questiondatabasetutorialannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Admin887
    wrote on last edited by
    #1

    i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:

    string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;

    string commandString = "Select id,cell from Events";

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);

    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "Events");

    DataTable datatable = ds.Tables[0];
    DataRow newRow = datatable.NewRow(); //adding a new Row

    newRow["id"] = "Gil";
    newRow["Event"] = "Gil2";

    datatable.Rows.Add(newRow);
    dataAdapter.Update(ds, "Events");
    ds.AcceptChanges();

    it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!

    Y E 2 Replies Last reply
    0
    • A Admin887

      i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:

      string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;

      string commandString = "Select id,cell from Events";

      OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);

      DataSet ds = new DataSet();
      dataAdapter.Fill(ds, "Events");

      DataTable datatable = ds.Tables[0];
      DataRow newRow = datatable.NewRow(); //adding a new Row

      newRow["id"] = "Gil";
      newRow["Event"] = "Gil2";

      datatable.Rows.Add(newRow);
      dataAdapter.Update(ds, "Events");
      ds.AcceptChanges();

      it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      you are not inserting new records. You are trying to update. But update what? here this should help ^

      Yusuf

      1 Reply Last reply
      0
      • A Admin887

        i did a application that creates a new and empty database file(MDB file). i want to create a new row in the new database. i did this:

        string connectionString = "provider=Microsoft.JET.OLEDB.4.0; " + "data source =" + Dir;

        string commandString = "Select id,cell from Events";

        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandString, connectionString);

        DataSet ds = new DataSet();
        dataAdapter.Fill(ds, "Events");

        DataTable datatable = ds.Tables[0];
        DataRow newRow = datatable.NewRow(); //adding a new Row

        newRow["id"] = "Gil";
        newRow["Event"] = "Gil2";

        datatable.Rows.Add(newRow);
        dataAdapter.Update(ds, "Events");
        ds.AcceptChanges();

        it throws me a exception: Update requires a valid InsertCommand when passed DataRow collection with new rows. What is Wrong?..i am not understand p.s All of this is in try catch... thank you!

        E Offline
        E Offline
        ElSpinos
        wrote on last edited by
        #3

        You will need to define the InsertCommand property of the DataAdapter like so:

        // Connection & DataAdapter initialization code should go here
        // ...

        OleDbCommand insertCommand = connection.CreateCommand();
        insertCommand.CommandText = "Insert Into Table(Col1, Col2, Col3) Values (?, ?, ?)";
        dataAdapter.InsertCommand = insertCommand;

        // ...

        You may also want to check out the UpdateCommand and DeleteCommand properties which will allow your adapter to update and delete records which is what it seems you are also trying to do in your code snippet.

        /F - .NET Developer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups