static void Main(string[] args)
        {
            string[,] fp = readFilePaths();
            OleDbConnection con = new OleDbConnection();
            OleDbDataAdapter da;
            DataSet ds = new DataSet();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/kyalanur/Desktop/Nav tool/PCGDataManager.mdb;User ID=;Password=;";
            string sq = "SELECT * FROM History";
            con.Open();
            da = new OleDbDataAdapter(sq, con);
            da.Fill(ds, "History");

            for (int i = 0; i < 284; i++)
            {
                if(true)
                {
                    //Enter into the dtabase
                    DataRow dr = ds.Tables["History"].NewRow();
                    dr[0] = DateTime.Today.Date.ToString();
                    dr[0] = fp[i,2];
                    dr[2] = generateFilePath(fp[i, 0], fp[i, 1]);
                    dr[3] = "  ";
                    dr[4] = "  ";
                    dr[5] = fp[i, 3];
                    ds.Tables["History"].Rows.Add(dr);
                    //OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                    //da.UpdateCommand = cb.GetUpdateCommand();
                    da.Update(ds, "History");
                }
                else
                {

                }
            }
        }

Edited 3 Years Ago by Dani: Formatting fixed

I want to insert records into the table History and I followed this process I found in some tutorial but I am getting this error. Please help me with this. i am a newbie to c#.

Use code tags please. :)

static void Main(string[] args)
        {
            string[,] fp = readFilePaths();
            OleDbConnection con = new OleDbConnection();
            OleDbDataAdapter da;
            DataSet ds = new DataSet();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/kyalanur/Desktop/Nav tool/PCGDataManager.mdb;User ID=;Password=;";
            string sq = "SELECT * FROM History";
            con.Open();
            da = new OleDbDataAdapter(sq, con);
            da.Fill(ds, "History");

            for (int i = 0; i < 284; i++)
            {
                if (true)
                {
                    //Enter into the dtabase
                    DataRow dr = ds.Tables["History"].NewRow();
                    dr[0] = DateTime.Today.Date.ToString();
                    dr[0] = fp[i, 2];
                    dr[2] = generateFilePath(fp[i, 0], fp[i, 1]);
                    dr[3] = " ";
                    dr[4] = " ";
                    dr[5] = fp[i, 3];
                    ds.Tables["History"].Rows.Add(dr);
                    //OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                    //da.UpdateCommand = cb.GetUpdateCommand();
                    da.Update(ds, "History");
                }
                else
                {

                }
            }
        }

I know absolutely nothing about databases so I'm pulling whatever I say straight from Google. :D

Lines 26 and 27 are commented out. Why? That is similar to what was suggested here.

Edited 5 Years Ago by WildBamaBoy: n/a

When you use a DataTable and call the Update() method one of three things can happen:

  • A row is added
  • A row is changed
  • A row is deleted

The adaptor needs to know how to do all these things but you have not told it how, thus the error. Put back in the two lines and add a third:

OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
da.InsertCommand = cb.GetInsertCommand();

You may also need to add the 'DeleteCommand'.

This article has been dead for over six months. Start a new discussion instead.