Update requires a valid InsertCommand when passed DataRow collection

Discussion in 'Microsoft C# .NET' started by Jon S via DotNetMonster.com, Jun 22, 2005.

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


    --
    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-csharp/200506/1
     
    Jon S via DotNetMonster.com, Jun 22, 2005
    #1
    1. Advertisements

  2. It's simple but please help!

    Jon S wrote:
    >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.



    --
    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-csharp/200506/1
     
    Jon S via DotNetMonster.com, Jun 22, 2005
    #2
    1. Advertisements

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

    --
    Sincerely,
    Dmytro Lapshyn [Visual Developer - Visual C# MVP]


    "Jon S via DotNetMonster.com" <> wrote in
    message news:...
    > 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.
    >
    >
    > --
    > Message posted via DotNetMonster.com
    > http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-csharp/200506/1
     
    Dmytro Lapshyn [MVP], Jun 22, 2005
    #3
  4. Hi Dmytro

    Thanks for that. I shall read up on it.

    Thanks.

    Dmytro Lapshyn [MVP] wrote:
    >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 all,
    >>

    >[quoted text clipped - 42 lines]
    >> I guess it's simple but I can't figure it out, please help???????
    >> Thanks, Jon.



    --
    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-csharp/200506/1
     
    Jon S via DotNetMonster.com, Jun 23, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    1
    Views:
    186
    chanmm
    Jun 7, 2006
  2. Venkat
    Replies:
    2
    Views:
    146
    ssamuel
    Oct 26, 2006
  3. lianqtlit
    Replies:
    4
    Views:
    598
    lianqtlit
    Dec 18, 2007
  4. Ryan Liu
    Replies:
    3
    Views:
    2,305
    RobinS
    Feb 2, 2008
  5. news.microsoft.com

    Garbage collection behavior for object passed out to COM event

    news.microsoft.com, Jun 4, 2009, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    178
    Jesse Houwing
    Jun 5, 2009
Loading...

Share This Page