Results 1 to 2 of 2

Thread: [2.0] Update requires a valid InsertCommand when passed DataRow collection with new r

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2006
    Posts
    719

    [2.0] Update requires a valid InsertCommand when passed DataRow collection with new r

    why this code doesnt work?

    Code:
                string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" + Application.StartupPath + "\\Employee.mdb";
                OleDbConnection Conn = new OleDbConnection(ConStr);
    
                Conn.Open();
    
                OleDbCommand Cmd = new OleDbCommand("Select * from Employee", Conn);
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
                DataSet dDataSet = new DataSet();
    	dAdapter.Fill(dDataSet, "Employee");
    
                DataRow AddNewRow = dDataSet.Tables["Employee"].NewRow();
                AddNewRow["EmployeeID"] = textBox1.Text;
                AddNewRow["EmployeeName"] = textBox2.Text;
    
                dDataSet.Tables["Employee"].Rows.Add(AddNewRow);
                dAdapter.Update(dDataSet, "Employee");
    
                Conn.Close();
    the error is:

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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Update requires a valid InsertCommand when passed DataRow collection with new r

    You can't insert a record into a database table without SQL code. That SQL code needs to be contained in the InsertCommand of your DataAdapter. Likewise the DeleteCommand and UpdateCommand control how records get deleted and updated. You need to create the Command yourself or else use an OleDbCommandBuilder.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width