0

On form close I call update but I got error message: update requires InsertCommand whereas I inspired from this tut http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace datatablemsaccess
{
    partial class Form1
    {
        OleDbDataAdapter dataAdapter;

        private string getSQL() {
            string sql = "SELECT * from tPerson";
            return sql;
        }

        private string getConnectionString() {

            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"App_Data\person.mdb";
            return connectionString;
        }

        private DataTable getDataTable(string sql)
        {
            DataTable dataTable;

            string connectionString = getConnectionString();

            using (dataAdapter = new OleDbDataAdapter(sql, connectionString))
            {
                dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
            }

            return dataTable;
        }
    }
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace datatablemsaccess
{
    public partial class Form1 : Form
    {
        BindingSource bindingSource;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = getSQL();

            bindingSource = new BindingSource();

            bindingSource.DataSource = getDataTable(sql);
            dataGridView1.DataSource = bindingSource;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            dataAdapter.Update((DataTable)bindingSource.DataSource);
        }
    }
}
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 1
    the error message is self explaining... you didn't defined an insertcommand. Add something like `dataAdapter.InsertCommand = connection.CreateCommand() ...`, or, my advice, use Visual STudio Wizard that produce 85% of the data access code – Steve B Jul 13 '11 at 13:54
  • I have just a datatable without dataset, i don't want wizard to generate full dataset it's overkilled – user310291 Jul 14 '11 at 19:26
  • with a bit of habit, you can use the designer and produces only what is required. – Steve B Jul 15 '11 at 07:33

1 Answers1

1

You need to define an InsertCommand.

See Update requires a valid InsertCommand when passed DataRow collection with new rows

Community
  • 1
  • 1
Armbrat
  • 2,295
  • 1
  • 23
  • 32