Update requires a valid InsertCommand when passed DataRow collection

  • Thread starter Thread starter Jon S via DotNetMonster.com
  • Start date Start date
J

Jon S via DotNetMonster.com

Hi all,

I'm having a problem updating a simple change I've made to a Access 2000
table through databinding. The error I get is :
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.data.dll
Additional information: Update requires a valid InsertCommand when
passed DataRow collection with
new rows.

The relevant code used is below :
public DataSet MakeConnection()
{
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;"+@"Data Source=A:\
ClientInfo.mdb;";
string select = "SELECT * FROM ClientInfo";
OleDbConnection conn = new OleDbConnection(source);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(select, conn);
this.dax = da;
*dax is a class level variable.
DataSet ds = new DataSet();
da.Fill(ds, "ClientInfo");
return ds;
}

private void btnNew_Click(object sender, System.EventArgs e)
{
DataRow row = ds.Tables["ClientInfo"].NewRow();
//row.BeginEdit();
*Have tried this line but still no joy.
row["Address1"] = txtAddress1.Text;
row["Address2"] = txtAddress2.Text;
row["Address3"] = txtAddress3.Text;
row["Address4"] = txtAddress4.Text;
ds.Tables["ClientInfo"].Rows.Add(row);
//row.EndEdit();
*Have tried this line but still no joy.
DataSet dsNew = ds.GetChanges(DataRowState.Added);
da.Update(dsNew, "ClientInfo");
*This is the line where the error (above) occurs!!!
ds.AcceptChanges();
}

I guess it's simple but I can't figure it out, please help???????
Thanks, Jon.
 
It's simple but please help!

Jon said:
Hi all,

I'm having a problem updating a simple change I've made to a Access 2000
table through databinding. The error I get is :
An unhandled exception of type 'System.InvalidOperationException'
occurred in system.data.dll
Additional information: Update requires a valid InsertCommand when
passed DataRow collection with
new rows.

The relevant code used is below :
public DataSet MakeConnection()
{
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;"+@"Data Source=A:\
ClientInfo.mdb;";
string select = "SELECT * FROM ClientInfo";
OleDbConnection conn = new OleDbConnection(source);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(select, conn);
this.dax = da;
*dax is a class level variable.
DataSet ds = new DataSet();
da.Fill(ds, "ClientInfo");
return ds;
}

private void btnNew_Click(object sender, System.EventArgs e)
{
DataRow row = ds.Tables["ClientInfo"].NewRow();
//row.BeginEdit();
*Have tried this line but still no joy.
row["Address1"] = txtAddress1.Text;
row["Address2"] = txtAddress2.Text;
row["Address3"] = txtAddress3.Text;
row["Address4"] = txtAddress4.Text;
ds.Tables["ClientInfo"].Rows.Add(row);
//row.EndEdit();
*Have tried this line but still no joy.
DataSet dsNew = ds.GetChanges(DataRowState.Added);
da.Update(dsNew, "ClientInfo");
*This is the line where the error (above) occurs!!!
ds.AcceptChanges();
}

I guess it's simple but I can't figure it out, please help???????
Thanks, Jon.
 
Hi Jon,

The data adapter needs to know the way to insert the added data row to the
Access data table. You basically have two options here: you can either
create an appropriate OleDbCommand in code and assign a reference to this
command to the adapter's InsertCommand, or you can instantiate the
OleDbCommandBuilder class which will generate the insert command for you.
Please refer to MSDN docs on the OleDbCommandBuilder class for the specific
requirements of its usage.
 
Hi Dmytro

Thanks for that. I shall read up on it.

Thanks.
Hi Jon,

The data adapter needs to know the way to insert the added data row to the
Access data table. You basically have two options here: you can either
create an appropriate OleDbCommand in code and assign a reference to this
command to the adapter's InsertCommand, or you can instantiate the
OleDbCommandBuilder class which will generate the insert command for you.
Please refer to MSDN docs on the OleDbCommandBuilder class for the specific
requirements of its usage.
[quoted text clipped - 42 lines]
I guess it's simple but I can't figure it out, please help???????
Thanks, Jon.
 
Back
Top