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 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.

beacuse it didnt work that way either.

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'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.