Editing a record (Access)

mza1979m

Member
Joined
Feb 20, 2003
Messages
13
Location
London, Ontario
I have a program that allows me to connect to an Access database (.mdb). I have no problems creating or removing records from within my program then updating them to the data source. My problem is how to modify existing records. It always gives me the following error message:

Update requires a valid UpdateCommand when passed DataRow collection with modified rows

In VB 6.0, I would use the statement datPersonal.Recordset.Edit (Where datPersonal is the name of my Data Control, which is no longer supported in VB.NET), then datPersonal.Recordset.Update.
So what code must I use to suit my purpose?

Any help would be appreciated.

PS. I hate Microsoft.
 
Got code? Show some of how your trying to edit. If your using dataadapter and dataset, just set your binding context to the record you want to edit, edit the fields and call an update. The adapter tracks changes and will update the database appropriatly. If your not using the dataadapter, you need to set the record you want to edit and then beginedit and endedit. Show us what your trying to do.
 
Yes, I guess I should have put how Im currently updating the database. Here goes...
I use the following to update the database:

(Im sorry, I dont know how to make the code in this message properly formatted/colour coded).

Code:
    Public Sub UpdateDataSet()
        Create a new dataset to hold the changes that have been made to the main dataset.
        Dim objDataSetChanges As Electronizer.DSElectronizer = New Electronizer.DSElectronizer()
        Stop any current edits.
        Me.BindingContext(objDSElectronizer, "Personal").EndCurrentEdit()
        Get the changes that have been made to the main dataset.
        objDataSetChanges = CType(objDSElectronizer.GetChanges, Electronizer.DSElectronizer)
        Check to see if any changes have been made.
        If (Not (objDataSetChanges) Is Nothing) Then
            Try
                There are changes that need to be made, so attempt to update the datasource by
                Calling the update method and passing the dataset and any parameters.
                Me.UpdateDataSource(objDataSetChanges)
                objDSElectronizer.Merge(objDataSetChanges)
                objDSElectronizer.AcceptChanges()
            Catch eUpdate As System.Exception
                Add your error handling code here.
                Throw eUpdate
            End Try
            Code to check the returned dataset for any errors that may have been pushed into the row objects error.
        End If
    End Sub
-----------------------------------

The following is used to load the data from the source:

-----------------------------------
    Public Sub LoadDataSet()
        Create a new dataset to hold the records returned from the call to FillDataSet.
        A temporary dataset is used because filling the existing dataset would
        require the databindings to be rebound.
        Dim objDataSetTemp As Electronizer.DSElectronizer
        objDataSetTemp = New Electronizer.DSElectronizer()
        Try
            Attempt to fill the temporary dataset.
            Me.FillDataSet(objDataSetTemp)
        Catch eFillDataSet As System.Exception
            Add your error handling code here.
            Throw eFillDataSet
        End Try
        Try
            Empty the old records from the dataset.
            objDSElectronizer.Clear()
            Merge the records into the main dataset.
            objDSElectronizer.Merge(objDataSetTemp)
        Catch eLoadMerge As System.Exception
            Add your error handling code here.
            Throw eLoadMerge
        End Try
    End Sub
-----------------------------------

...And Ill also include the following subroutines just in case:

-----------------------------------
    Public Sub UpdateDataSource(ByVal ChangedRows As Electronizer.DSElectronizer)
        Try
            The data source only needs to be updated if there are changes pending.
            If (Not (ChangedRows) Is Nothing) Then
                Open the connection.
                Me.OleDbConnection1.Open()
                Attempt to update the data source.
                OleDbDataAdapter1.Update(ChangedRows)
                OleDbDataAdapter2.Update(ChangedRows)
            End If
        Catch updateException As System.Exception
            Add your error handling code here.
            Throw updateException
        Finally
            Close the connection whether or not the exception was thrown.
            Me.OleDbConnection1.Close()
        End Try
    End Sub
-----------------------------------

-----------------------------------
    Public Sub FillDataSet(ByVal dataSet As Electronizer.DSElectronizer)
        Turn off constraint checking before the dataset is filled.
        This allows the adapters to fill the dataset without concern
        for dependencies between the tables.
        dataSet.EnforceConstraints = False
        Try
            Open the connection.
            Me.OleDbConnection1.Open()
            Attempt to fill the dataset through the OleDbDataAdapter1.
            Me.OleDbDataAdapter1.Fill(dataSet)
            Me.OleDbDataAdapter2.Fill(dataSet)
        Catch fillException As System.Exception
            Add your error handling code here.
            Throw fillException
        Finally
            Turn constraint checking back on.
            dataSet.EnforceConstraints = True
            Close the connection whether or not the exception was thrown.
            Me.OleDbConnection1.Close()
        End Try
    End Sub
To load the data from the database into the text boxes, I use the following code:

Me.LoadDataSet()
 
Last edited by a moderator:
Code:
A temporary dataset is used because filling the existing dataset would
        require the databindings to be rebound.

Maybe a naive question, but, why?

Ive was not vb6 programer in the past. Im uncorrupted I suppose
I think your assuming vb.net works like vb6.
 
I assume your binding context is to a dataset not the data source.
The connection is opened and closed as necessary via the dataadapter in vb.net, you dont need to open and close a connection
except when using a datareader from the data source.
Once your dataset is filled, you can manipulate your data and add rows, delete rows and edit rows (although simply changing the
field (row.item) will change the dataset which leaves a tag in the
dataadapter that the row has been added, deleted, or edited ), then
when you call an update of the dataadapter, it will update your data source appropriately.
Your binding context shouldnt need to be rebound. You can add binding to a field programatically:
Code:
lblEmployeeName.DataBindings.Add("text", dataset1, "Employees.Name")
Seems like your subs are doing a lot of the work that the adapter is
designed to do.

Hope that helps.
 
hi,

this error
Update requires a valid UpdateCommand when passed DataRow collection with modified rows

is flasehd when you are trying to insert /update/delete a record in a dataset and you have not set the appropricate commands for the dataadpater used for filling the table.

i suggest sfter you set the select command for the dataadapter you use a command builder boject to fill all the remaining commands for insert/update/delete.

then it will work fine.

Hemen Kapadia
 
Back
Top