none
UnsafeNativeMethods.ICommandText.Execute after update RRS feed

  • Question

  • I have a form with a datagridview. after doubleclick another form showes up with the selected record. I make some changes and click OK. The form closes and come back in the main programme. Then I click the save button on the toolbar of the datagrid and an errormessage occurres with above message.
    The programma doesn't update the database.

    The programme setup looks like the StoreTracker Programme, at the moment I'm busy with module 2546 Data Access.
    Using VS.NET 2008.

    Private Sub DgvSRVUUR_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgvSRVUUR.CellDoubleClick
    
            Dim pkValue(5) As String
    
            pkValue(0) = Me.DgvSRVUUR.CurrentRow.Cells("WERKNEMERNR").Value
    
            pkValue(1) = Me.DgvSRVUUR.CurrentRow.Cells("WEEKNUMMER").Value
    
            pkValue(2) = Me.DgvSRVUUR.CurrentRow.Cells("DAGNUMMER").Value
    
            pkValue(3) = Me.DgvSRVUUR.CurrentRow.Cells("ORDERNUMMER").Value
    
            pkValue(4) = Me.DgvSRVUUR.CurrentRow.Cells("BONNUMMER").Value
    
            pkValue(5) = Me.DgvSRVUUR.CurrentRow.Cells("KOSTENPLAATSNUMMER").Value
    
    
    
            Dim SRVUURrow As DataRow = Me.DsSRVUUR.Tables("SRVUUR").Rows.Find(pkValue)
    
    
    
            Dim frmUUR As New frmUren(Me.DsOrdOvz, SRVUURrow)
    
            frmUUR.Show()
    
        End Sub
    
    
    Imports ORDSRV2008.DsOrdOvz
    
    Imports System.Data
    
    Imports System.Data.OleDb
    
    Imports System.Data.DbType
    
    Imports Pervasive.Data.SqlClient
    
    
    
    Namespace DataAcces
    
    
    
        Friend NotInheritable Class SRVUURDAC
    
    
    
    #Region "declareren dataadapter"
    
    
    
            Private Shared srvuurDataAdapter As OleDbDataAdapter = CreatesrvuurDataAdapter()
    
    
    
    #End Region
    
    
    
    #Region "Methods to create a DataSet, to add DataTables, and to define the schema for the DataTables"
    
    
    
            ''' <summary>
    
            ''' Creates a DataSet object, adds "SalesPerson" and "Store" DataTables, and defines their schema and relationship.
    
            ''' </summary>
    
            ''' <returns>DataSet object, containing "SalesPerson" and "Store" DataTables</returns>
    
            Private Shared Function CreateSRVUURDataset() As DataSet
    
    
    
                ' Create a DataSet
    
                Dim DsSRVUUR As New DataSet()
    
    
    
                ' Add DataTables to the DataSet
    
                Dim SRVUURTable As DataTable = DsSRVUUR.Tables.Add("SRVUUR")
    
    
    
                ' Define schema for the DataTables
    
                DefineSRVUURTableSchema(SRVUURTable)
    
    
    
                ' Define constraints and relations for the DataTables
    
                'DefineConstraintsAndRelations(DsSRVUUR)
    
    
    
                ' Return the DataSet
    
                Return DsSRVUUR
    
    
    
            End Function
    
    
    
            ''' <summary>
    
            ''' Defines the schema for the SalesPerson DataTable.
    
            ''' </summary>
    
            ''' <param name="table">"SalesPerson" DataTable</param>
    
            Private Shared Sub DefineSRVUURTableSchema(ByVal table As DataTable)
    
                ' Add the WERKNEMER column
    
                Dim WERKNEMERColumn As DataColumn = table.Columns.Add("WERKNEMERNR", GetType(String))
    
                WERKNEMERColumn.AllowDBNull = False
    
    
    
                ' Add the WEEKNUMMER column
    
                Dim WEEKNUMMERColumn As DataColumn = table.Columns.Add("WEEKNUMMER", GetType(Integer))
    
                WEEKNUMMERColumn.AllowDBNull = False
    
    
    
                ' Add the DAGNUMMER column
    
                Dim DAGNUMMERColumn As DataColumn = table.Columns.Add("DAGNUMMER", GetType(Short))
    
                DAGNUMMERColumn.AllowDBNull = False
    
    
    
                ' Add the ORDERNUMMER column
    
                Dim ORDERNUMMERColumn As DataColumn = table.Columns.Add("ORDERNUMMER", GetType(Integer))
    
                DAGNUMMERColumn.AllowDBNull = False
    
    
    
                ' Add the BONNUMMER column
    
                Dim BONNUMMERColumn As DataColumn = table.Columns.Add("BONNUMMER", GetType(Integer))
    
                BONNUMMERColumn.AllowDBNull = False
    
    
    
                ' Add the KOSTENPLAATSNUMMER column
    
                Dim KOSTENPLAATSNUMMERColumn As DataColumn = table.Columns.Add("KOSTENPLAATSNUMMER", GetType(String))
    
                KOSTENPLAATSNUMMERColumn.AllowDBNull = False
    
    
    
                ' Add the UREN column
    
                Dim URENColumn As DataColumn = table.Columns.Add("UREN", GetType(Int16))
    
    
    
                table.PrimaryKey = New DataColumn() {WERKNEMERColumn, WEEKNUMMERColumn, DAGNUMMERColumn, ORDERNUMMERColumn, BONNUMMERColumn, KOSTENPLAATSNUMMERColumn}
    
                
    
            End Sub
    
          
    
    #End Region
    
    
    
    #Region "Declarations of data adapters"
    
    
    
            Private Shared Function CreatesrvuurDataAdapter() As OleDbDataAdapter
    
                Dim sql As String
    
                ' Get a connection object
    
                Dim connection As OleDbConnection = DataConnection.ConnectionManager.GetConnection()
    
    
    
                ' Create a data adapter
    
                Dim dataAdapter As New OleDbDataAdapter()
    
                dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
    
    
                ' Add the SelectCommand
    
                sql = "SELECT WERKNEMERNR,WEEKNUMMER,DAGNUMMER,ORDERNUMMER,BONNUMMER,KOSTENPLAATSNUMMER,UREN FROM SRVUUR"
    
                dataAdapter.SelectCommand = New OleDbCommand(sql, connection)
    
                dataAdapter.SelectCommand.CommandType = CommandType.Text
    
    
    
                ' Add the InsertCommand
    
                sql = "INSERT INTO SRVUUR (WERKNEMERNR,WEEKNUMMER,DAGNUMMER,ORDERNUMMER,BONNUMMER,KOSTENPLAATSNUMMER,UREN)" & _
    
                "VALUES (@WERKNEMERNR,@WEEKNUMMER,@DAGNUMMER,@ORDERNUMMER" & _
    
                "@BONNUMMER,@KOSTENPLAATSNUMMER,@UREN"
    
                dataAdapter.InsertCommand = New OleDb.OleDbCommand(sql, connection)
    
                dataAdapter.InsertCommand.CommandType = CommandType.Text
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@WEEKNUMMER", OleDbType.Integer).SourceColumn = "WEEKNUMMER"
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@DAGNUMMER", OleDbType.SmallInt).SourceColumn = "DAGNUMMER"
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@ORDERNUMMER", OleDbType.Integer).SourceColumn = "ORDERNUMMER"
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@BONNUMMER", OleDbType.Integer).SourceColumn = "BONNUMMER"
    
                dataAdapter.InsertCommand.Parameters.AddWithValue("@KOSTENPLAATSNUMMER", OleDbType.Char).SourceColumn = "KOSTENPLAATSNUMMER"
    
    
    
                ' Add the UpdateCommand
    
                sql = "UPDATE SRVUUR SET WERKNEMERNR = @WERKNEMERNR, WEEKNUMMER = @WEEKNUMMER, DAGNUMMER = @DAGNUMMER, ORDERNUMMER =@ORDERNUMMER" & _
    
                ",BONNUMMER = @BONNUMMER, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER, UREN = @UREN WHERE (WERKNEMERNR = @WERKNEMERNR,WEEKNUMMER = @WEEKNUMMER, DAGNUMMER = @DAGNUMMER, ORDERNUMMER = @ORDERNUMMER, BONNUMMER = @BONNUMMER, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER)"
    
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
    
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WEEKNUMMER", OleDbType.Integer).SourceColumn = "WEEKNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@DAGNUMMER", OleDbType.SmallInt).SourceColumn = "DAGNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@ORDERNUMMER", OleDbType.Integer).SourceColumn = "ORDERNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@BONNUMMER", OleDbType.Integer).SourceColumn = "BONNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@KOSTENPLAATSNUMMER", OleDbType.Char).SourceColumn = "KOSTENPLAATSNUMMER"
    
    
    
                ' Add the DeleteCommand
    
                sql = "DELETE FROM SRVUUR WHERE WHERE (WERKNEMERNR = @WERKNEMERNR,WEEKNUMMER = @WEEKNUMMER, DAGNUMMER = @DAGNUMMER, ORDERNUMMER = @ORDERNUMMER, BONNUMMER = @BONNUMMER, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER)"
    
                dataAdapter.DeleteCommand = New OleDbCommand(sql, connection)
    
                dataAdapter.DeleteCommand.CommandType = CommandType.Text
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WEEKNUMMER", OleDbType.Integer).SourceColumn = "WEEKNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@DAGNUMMER", OleDbType.SmallInt).SourceColumn = "DAGNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@ORDERNUMMER", OleDbType.Integer).SourceColumn = "ORDERNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@BONNUMMER", OleDbType.Integer).SourceColumn = "BONNUMMER"
    
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@KOSTENPLAATSNUMMER", OleDbType.Char).SourceColumn = "KOSTENPLAATSNUMMER"
    
    
    
                ' Return the data adapter
    
                Return dataAdapter
    
    
    
            End Function
    
    
    
    
    
    #End Region
    
    
    
    #Region "Methods to get a populated DataSet, and to save a DataSet"
    
    
    
            ''' <summary>
    
            ''' Returns a DataSet containing SalesPerson and Store data.
    
            ''' </summary>
    
            ''' <returns>DataSet object</returns>
    
            Public Shared Function GetData() As DataSet
    
    
    
                ' Create a DataSet object
    
                Dim dsSRVUUR As DataSet = CreateSRVUURDataSet()
    
    
    
                ' Fill the "SalesPerson" and "Store" DataTables
    
                dsSRVUUR.EnforceConstraints = False
    
                Try
    
                    srvuurDataAdapter.Fill(dsSRVUUR.Tables("SRVUUR"))
    
                    'dsSRVUUR.EnforceConstraints = True
    
                Catch ex As Exception
    
                    MessageBox.Show(ex.StackTrace)
    
                    MessageBox.Show(ex.Message)
    
                    MessageBox.Show(ex.Source)
    
                End Try
    
    
    
    
    
                ' Return the DataSet
    
                Return dsSRVUUR
    
    
    
            End Function
    
    
    
            ''' <summary>
    
            ''' Saves DataSet data to the database. Merges data updates (from the database) back into the DataSet.
    
            ''' </summary>
    
            ''' <param name="changesDataSet">DataSet object</param>
    
            Public Shared Sub SaveData(ByRef changesDataSet As DataSet)
    
    
    
                ' Save the Modified rows (if any) in the "SalesPerson" and "Store" DataTables, and merge-in data from the database
    
                Try
    
                    Dim modifiedDataSet As DataSet = changesDataSet.GetChanges(DataRowState.Modified)
    
                    If Not (modifiedDataSet Is Nothing) Then
    
    
    
                        srvuurDataAdapter.Update(modifiedDataSet.Tables("SRVUUR"))
    
                        changesDataSet.Merge(modifiedDataSet)
    
    
    
                    End If
    
    
    
                Catch ex As Exception
    
                    MessageBox.Show(ex.StackTrace)
    
                    MessageBox.Show(ex.Message)
    
                    MessageBox.Show(ex.Source)
    
                End Try
    
                
    
            End Sub
    
    
    
    #End Region
    
    
    
        End Class
    
    
    
    End Namespace
    
    
    #Region "Save uren button"
    
    
    
        Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
    
            Try
    
                If Me.DsSRVUUR.HasChanges() Then
    
                    ' Get a copy of the DataSet, which contains only the changes
    
                    Dim changesDataSet As DataSet = Me.DsSRVUUR.GetChanges()
    
    
    
                    ' Save the changes to the database (and get back an up-to-date DataSet)
    
                    SRVUURDAC.SaveData(changesDataSet)
    
    
    
                    ' Merge any data updates (such as timestamps) back into the dataset
    
                    DsSRVUUR.Merge(SRVUURDAC.GetData())
    
    
    
                    ' Accept all changes, to mark the DataSet as "clean"
    
                    DsSRVUUR.AcceptChanges()
    
    
    
                    MessageBox.Show("Data saved successfully.", "Save Changes")
    
                End If
    
    
    
    
    
    
    
            Catch ex As OleDbException
    
    
    
                MessageBox.Show("Data not saved.", "Save Changes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
    
    
            End Try
    
        End Sub
    
    
    
    #End Region
    
    
       bij System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
       bij System.Data.OleDb.OleDbCommand.ExecuteCommandTextForMultpleResults(tagDBPARAMS dbParams, Object& executeResult)
       bij System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       bij System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       bij System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       bij System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       bij System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       bij System.Data.Common.DbDataAdapter.UpdateRowExecute(RowUpdatedEventArgs rowUpdatedEvent, IDbCommand dataCommand, StatementType cmdIndex)
       bij System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       bij System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       bij System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
       bij ORDSRV2008.DataAcces.SRVUURDAC.SaveData(DataSet& changesDataSet) in C:\Documents and Settings\ivo\Mijn documenten\Visual Studio 2008\Projects\ORDSRV2008\ORDSRV2008\DataAcces.vb:regel 210
    Friday, January 22, 2010 9:10 AM

Answers

  • Alex,

    I checked my sample with your suggestion, changed my code to:

    ' Add the UpdateCommand
    
                sql = "UPDATE SRVUUR SET UREN = @UREN WHERE WERKNEMERNR = @prmWERKNEMERNR, WEEKNUMMER = @prmWEEKNUMMER, DAGNUMMER = @prmDAGNUMMER" & _
    
                ", ORDERNUMMER = @prmORDERNUMMER, BONNUMMER = @prmBONNUMMER, KOSTENPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"
    
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
    
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
    
                dataAdapter.UpdateCommand.Parameters.Add("@UREN", OleDbType.SmallInt, Int16, "UREN")
    
                
    
                Dim pt As OleDbParameter
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, Int32, "WEEKNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, Int16, "DAGNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, Int32, "ORDERNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, Int32, "BONNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
    
                pt.SourceVersion = DataRowVersion.Original
    
    
    Hopes this makes more sense. The pgm still gives the error of UnSaveNativeMethods.ICommand.Execute.
    Poging tot het lezen of schrijven van beveiligd geheugen...Attempt to read/write of protected memory

    Hi Keereweer,

    The third parameter of the Add() method is an expression, but you assign a type(such as Int32,Int16) to it. In addition, in sql statement, you need to use "and" between two query conditions. For example:

    sql = "UPDATE SRVUUR SET UREN = @UREN WHERE WERKNEMERNR = @prmWERKNEMERNR And WEEKNUMMER = @prmWEEKNUMMER And DAGNUMMER = @prmDAGNUMMER And ORDERNUMMER = @prmORDERNUMMER And BONNUMMER = @prmBONNUMMER And KOSTE
    NPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"

    Please add pt.SourceVersion = DataRowVersion.Original for every parameter who will use their original value.

                Dim pt As OleDbParameter

                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, Int32, "WEEKNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, Int16, "DAGNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, Int32, "ORDERNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, Int32, "BONNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
                pt.SourceVersion = DataRowVersion.Original

    The error message "Attempted to read or write protected memory"  is often an indication that other memory is corrupt. Cause of
    the error are various. Please check the parameter type and value carefully first.

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 27, 2010 6:36 AM
    Moderator
  • Hi Alex,

    So as we stated before it must have something to do with the settings of the adapter.
    I have been doing other things lately. Today I thought to give it a shot. Suddenly my eyes opened and noticed de Deletecommand.
    It's updating the Update command!. I corrected the statements.
    Now the update is doing perfect. You must have noticed this typical copy move.

    dataAdapter.DeleteCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.VarChar, 6, "WERKNEMERNR")
                dataAdapter.DeleteCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.[Integer], 10, "WEEKNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.[Integer], 5, "DAGNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.[Integer], 10, "ORDERNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmBONNUMMER", OleDbType.[Integer], 10, "BONNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.VarChar, 4, "KOSTENPLAATSNUMMER")

    Thanx a lot for your comment.
    • Marked as answer by Keereweer Tuesday, March 2, 2010 4:08 PM
    Tuesday, March 2, 2010 4:08 PM

All replies

  • Hi Keereweer,

    Could you please insert some breakpoints in each sub, press F11 to debug and tell us at which statement the error occurs?
    That will be much helpful for solving your problem. Thank you.

    Best regards,
    Alex Liang

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 25, 2010 11:29 AM
    Moderator
  • Hi,

    It is the statement: SRVUURDAC.SaveData(changesDataSet)

    This weekend just for trying I changed the statement for the update parameters to:
    dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR", OleDbType.Char).Value = "WERKNEMERNR"
    dataAdapter.UpdateCommand.Parameters.Add("@WEEKNUMMER", OleDbType.Integer).Value = "WEEKNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@DAGNUMMER", OleDbType.SmallInt).Value = "DAGNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@ORDERNUMMER", OleDbType.Integer).Value = "ORDERNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@BONNUMMER", OleDbType.Integer).Value = "BONNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@KOSTENPLAATSNUMMER", OleDbType.Char).Value = "KOSTENPLAATSNUMMER"

    When running the pgm there's an error message on the same SaveData statement: Problem converting String to Int32.

    Maybe the whole problem has something to do with the declaration of the update statement?
     
    Best regards,
    Ivo Keereweer

    Monday, January 25, 2010 1:00 PM
  • Hi,

    It is the statement: SRVUURDAC.SaveData(changesDataSet)

    This weekend just for trying I changed the statement for the update parameters to:
    dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR", OleDbType.Char).Value = "WERKNEMERNR"
    dataAdapter.UpdateCommand.Parameters.Add("@WEEKNUMMER", OleDbType.Integer).Value = "WEEKNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@DAGNUMMER", OleDbType.SmallInt).Value = "DAGNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@ORDERNUMMER", OleDbType.Integer).Value = "ORDERNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@BONNUMMER", OleDbType.Integer).Value = "BONNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@KOSTENPLAATSNUMMER", OleDbType.Char).Value = "KOSTENPLAATSNUMMER"

    When running the pgm there's an error message on the same SaveData statement: Problem converting String to Int32.

    Maybe the whole problem has something to do with the declaration of the update statement?
     
    Best regards,
    Ivo Keereweer


    Hi Keereweer,

    The WEEKNUMMER column's data type is Integer, but you assign a string to it, that leads to the error "converting String to Int32".
    As you have said, "the whole problem has something to do with the declaration of the update statement", I have the same thought.

                ' Add the UpdateCommand
                sql = "UPDATE SRVUUR SET WERKNEMERNR = @WERKNEMERNR, WEEKNUMMER = @WEEKNUMMER, DAGNUMMER = @DAGNUMMER, ORDERNUMMER =@ORDERNUMMER" & _
                            ",BONNUMMER = @BONNUMMER, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER, UREN = @UREN WHERE (WERKNEMERNR = @WERKNEMERNR,WEEKNUMMER = @WEEKNUMMER, DAGNUMMER = @DAGNUMMER, ORDERNUMMER = @ORDERNUMMER, BONNUMMER = @BONNUMMER, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER)"
    
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@WEEKNUMMER", OleDbType.Integer).SourceColumn = "WEEKNUMMER"
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@DAGNUMMER", OleDbType.SmallInt).SourceColumn = "DAGNUMMER"
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@ORDERNUMMER", OleDbType.Integer).SourceColumn = "ORDERNUMMER"
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@BONNUMMER", OleDbType.Integer).SourceColumn = "BONNUMMER"
                dataAdapter.UpdateCommand.Parameters.AddWithValue("@KOSTENPLAATSNUMMER", OleDbType.Char).SourceColumn = "KOSTENPLAATSNUMMER"
    In your sql statement, I notice that the parameter name in Set clause is the same as in Where clause. For example,

    @WEEKNUMMER in SET clause is the same as @WEEKNUMMER in Where clause. This is OK in compile time, but may produce errors in run time.  I do not understand why you declare it like that. It seems to me that they should be @WEEKNUMMER_NEW in Set clause and @WEEKNUMMER_OLD in Where clause.

    Please change the update statement to the ones as the followings and try again:

                ' Add the UpdateCommand
                sql = "UPDATE SRVUUR SET WERKNEMERNR = @WERKNEMERNR_new, WEEKNUMMER = @WEEKNUMMER_new, DAGNUMMER = @DAGNUMMER_new, ORDERNUMMER =@ORDERNUMMER_new" & _
                            ",BONNUMMER = @BONNUMMER_new, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER_new, UREN = @UREN_new WHERE (WERKNEMERNR = @WERKNEMERNR_old,WEEKNUMMER = @WEEKNUMMER_old, DAGNUMMER = @DAGNUMMER_old, ORDERNUMMER = @ORDERNUMMER_old, BONNUMMER = @BONNUMMER_old, KOSTENPLAATSNUMMER = @KOSTENPLAATSNUMMER_old)"
    
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
                dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR_new", OleDbType.Integer, 0, "WERKNEMERNR")
                'Add other parameters in SET clause here
    
                Dim pt As OleDbParameter
                pt = dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR_old", OleDbType.Integer, 0, "WERKNEMERNR")
                pt.SourceVersion = DataRowVersion.Original
                'Add other parameters in WHEREE clause here
    Best regards,
    Alex Liang

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 26, 2010 3:48 AM
    Moderator
  • Alex,

    Your suggestion is a bit different compared to the example I used.

    Example:' Add the UpdateCommand
                dataAdapter.UpdateCommand = New SqlCommand("uspUpdateSalesPerson", connection)
                dataAdapter.UpdateCommand.CommandType = CommandType.StoredProcedure
                dataAdapter.UpdateCommand.Parameters.Add("@prmSalesPersonID", SqlDbType.Int).SourceColumn = "SalesPersonID"
                dataAdapter.UpdateCommand.Parameters.Add("@prmTerritoryID", SqlDbType.Int).SourceColumn = "TerritoryID"
                dataAdapter.UpdateCommand.Parameters.Add("@prmSalesQuota", SqlDbType.Money).SourceColumn = "SalesQuota"
                dataAdapter.UpdateCommand.Parameters.Add("@prmBonus", SqlDbType.Money).SourceColumn = "Bonus"
                dataAdapter.UpdateCommand.Parameters.Add("@prmCommissionPct", SqlDbType.SmallMoney).SourceColumn = "CommissionPct"
                dataAdapter.UpdateCommand.Parameters.Add("@prmSalesYTD", SqlDbType.Money).SourceColumn = "SalesYTD"
                dataAdapter.UpdateCommand.Parameters.Add("@prmSalesLastYear", SqlDbType.Money).SourceColumn = "SalesLastYear"

    You are certainly right declaring a sourcecolumn or sourceversion. That's what my example also does.
    My example uses a stored procedure. I changed that to CommandType.Text for using a sql string statement. But then the pgm came up with the error UnsafeNativeMethods thing.

    Why is the storedprocedure not causing any troubles during update.?
    /****** Create uspUpdateSalesPerson stored procedure ******/
    ALTER PROCEDURE dbo.uspUpdateSalesPerson
    	(
    	@prmSalesPersonID int,
    	@prmTerritoryID int,
    	@prmSalesQuota money,
    	@prmBonus money,
    	@prmCommissionPct smallmoney,
    	@prmSalesYTD money,
    	@prmSalesLastYear money
    	)
    AS
    	UPDATE Sales.SalesPerson
    		SET TerritoryID=@prmTerritoryID, SalesQuota=@prmSalesQuota, Bonus=@prmBonus, CommissionPct=@prmCommissionPct, SalesYTD=@prmSalesYTD, SalesLastYear=@prmSalesLastYear
    	WHERE
    		SalesPersonID=@prmSalesPersonID
    
     
    Tuesday, January 26, 2010 8:51 AM
  • Hi Keereweer,

    In codes of your first post, you use

    dataAdapter.UpdateCommand.Parameters.AddWithValue("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR".

    which I think is supposed to be

    dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"  or
    dataAdapter.UpdateCommand.Parameters.AddWithValue("@WERKNEMERNR", parametervalue).

    In weekend you tried the following statements which leads to the error "can not convert from string to int32":

    dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR", OleDbType.Char).Value = "WERKNEMERNR"
    dataAdapter.UpdateCommand.Parameters.Add("@WEEKNUMMER", OleDbType.Integer).Value = "WEEKNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@DAGNUMMER", OleDbType.SmallInt).Value = "DAGNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@ORDERNUMMER", OleDbType.Integer).Value = "ORDERNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@BONNUMMER", OleDbType.Integer).Value = "BONNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@KOSTENPLAATSNUMMER", OleDbType.Char).Value = "KOSTENPLAATSNUMMER"

    I think they should be

    dataAdapter.UpdateCommand.Parameters.Add("@WERKNEMERNR", OleDbType.Char).SourceColumn = "WERKNEMERNR"
    dataAdapter.UpdateCommand.Parameters.Add("@WEEKNUMMER", OleDbType.Integer).SourceColumn = "WEEKNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@DAGNUMMER", OleDbType.SmallInt).SourceColumn = "DAGNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@ORDERNUMMER", OleDbType.Integer).SourceColumn = "ORDERNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@BONNUMMER", OleDbType.Integer).SourceColumn = "BONNUMMER"
    dataAdapter.UpdateCommand.Parameters.Add("@KOSTENPLAATSNUMMER", OleDbType.Char).SourceColumn="KOSTENPLAATSNUMMER"

    In addition, just as I have mentioned in the previous post, all the parameters in the sql statement are redeclared, which would cause exceptions in run time. So could you change them and run the program to find whether the error occurs?

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 26, 2010 12:42 PM
    Moderator
  • Alex,

    I checked my sample with your suggestion, changed my code to:

    ' Add the UpdateCommand
                sql = "UPDATE SRVUUR SET UREN = @UREN WHERE WERKNEMERNR = @prmWERKNEMERNR, WEEKNUMMER = @prmWEEKNUMMER, DAGNUMMER = @prmDAGNUMMER" & _
                ", ORDERNUMMER = @prmORDERNUMMER, BONNUMMER = @prmBONNUMMER, KOSTENPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
                dataAdapter.UpdateCommand.Parameters.Add("@UREN", OleDbType.SmallInt, Int16, "UREN")
                
                Dim pt As OleDbParameter
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, Int32, "WEEKNUMMER")
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, Int16, "DAGNUMMER")
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, Int32, "ORDERNUMMER")
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, Int32, "BONNUMMER")
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
                pt.SourceVersion = DataRowVersion.Original
    Hopes this makes more sense. The pgm still gives the error of UnSaveNativeMethods.ICommand.Execute.
    Poging tot het lezen of schrijven van beveiligd geheugen...Attempt to read/write of protected memory
    Tuesday, January 26, 2010 1:37 PM
  • Alex,

    I checked my sample with your suggestion, changed my code to:

    ' Add the UpdateCommand
    
                sql = "UPDATE SRVUUR SET UREN = @UREN WHERE WERKNEMERNR = @prmWERKNEMERNR, WEEKNUMMER = @prmWEEKNUMMER, DAGNUMMER = @prmDAGNUMMER" & _
    
                ", ORDERNUMMER = @prmORDERNUMMER, BONNUMMER = @prmBONNUMMER, KOSTENPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"
    
                dataAdapter.UpdateCommand = New OleDb.OleDbCommand(sql, connection)
    
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
    
                dataAdapter.UpdateCommand.Parameters.Add("@UREN", OleDbType.SmallInt, Int16, "UREN")
    
                
    
                Dim pt As OleDbParameter
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, Int32, "WEEKNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, Int16, "DAGNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, Int32, "ORDERNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, Int32, "BONNUMMER")
    
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
    
                pt.SourceVersion = DataRowVersion.Original
    
    
    Hopes this makes more sense. The pgm still gives the error of UnSaveNativeMethods.ICommand.Execute.
    Poging tot het lezen of schrijven van beveiligd geheugen...Attempt to read/write of protected memory

    Hi Keereweer,

    The third parameter of the Add() method is an expression, but you assign a type(such as Int32,Int16) to it. In addition, in sql statement, you need to use "and" between two query conditions. For example:

    sql = "UPDATE SRVUUR SET UREN = @UREN WHERE WERKNEMERNR = @prmWERKNEMERNR And WEEKNUMMER = @prmWEEKNUMMER And DAGNUMMER = @prmDAGNUMMER And ORDERNUMMER = @prmORDERNUMMER And BONNUMMER = @prmBONNUMMER And KOSTE
    NPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"

    Please add pt.SourceVersion = DataRowVersion.Original for every parameter who will use their original value.

                Dim pt As OleDbParameter

                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, Int32, "WEEKNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, Int16, "DAGNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, Int32, "ORDERNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, Int32, "BONNUMMER")
                pt.SourceVersion = DataRowVersion.Original
                pt = dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
                pt.SourceVersion = DataRowVersion.Original

    The error message "Attempted to read or write protected memory"  is often an indication that other memory is corrupt. Cause of
    the error are various. Please check the parameter type and value carefully first.

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 27, 2010 6:36 AM
    Moderator
  • Hi Keereweer,

    Did my solution solve your problem? If no and the error still occurs, could you please supply some more detail information(such as the whole error message) about the error "Attempt to read/write of protected memory"?

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 29, 2010 9:16 AM
    Moderator
  • Hi Alex,

    After you last reply, I went back to my source and spent the last week searching internet, debugging and trying to get an answer for my vaque problem. It didn't help.
    Here's my source of the complete namespace together with the connection statements and your error message request. Dont mention the comments because i copied some things.

    Imports ORDSRV2008.DsOrdOvz
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Data.DbType
    Imports Pervasive.Data.SqlClient
    
    Namespace DataAcces
    
        Friend NotInheritable Class SRVUURDAC
    
    #Region "declareren dataadapter"
    
            Private Shared srvuurDataAdapter As OleDbDataAdapter = CreatesrvuurDataAdapter()
    
    #End Region
    
    #Region "Methods to create a DataSet, to add DataTables, and to define the schema for the DataTables"
    
            ''' <summary>
            ''' Creates a DataSet object, adds "SalesPerson" and "Store" DataTables, and defines their schema and relationship.
            ''' </summary>
            ''' <returns>DataSet object, containing "SalesPerson" and "Store" DataTables</returns>
            Private Shared Function CreateSRVUURDataset() As DataSet
    
                ' Create a DataSet
                Dim DsSRVUUR As New DataSet()
    
                ' Add DataTables to the DataSet
                Dim SRVUURTable As DataTable = DsSRVUUR.Tables.Add("SRVUUR")
    
                ' Define schema for the DataTables
                DefineSRVUURTableSchema(SRVUURTable)
    
                ' Define constraints and relations for the DataTables
                'DefineConstraintsAndRelations(DsSRVUUR)
    
                ' Return the DataSet
                Return DsSRVUUR
    
            End Function
    
            ''' <summary>
            ''' Defines the schema for the SalesPerson DataTable.
            ''' </summary>
            ''' <param name="table">"SalesPerson" DataTable</param>
            Private Shared Sub DefineSRVUURTableSchema(ByVal table As DataTable)
                ' Add the WERKNEMER column
                Dim Werknemer As DataColumn = table.Columns.Add("WERKNEMERNR", System.Type.GetType("System.String"))
                Werknemer.AllowDBNull = False
                Werknemer.MaxLength = 6
    
                ' Add the WEEKNUMMER column
                Dim weeknummer As DataColumn = table.Columns.Add("WEEKNUMMER", System.Type.GetType("System.Int32"))
                weeknummer.AllowDBNull = False
    
                ' Add the DAGNUMMER column
                Dim dagnummer As DataColumn = table.Columns.Add("DAGNUMMER", System.Type.GetType("System.Int16"))
                dagnummer.AllowDBNull = False
    
                ' Add the ORDERNUMMER column
                Dim ordernummer As DataColumn = table.Columns.Add("ORDERNUMMER", System.Type.GetType("System.Int32"))
                ordernummer.AllowDBNull = False
    
                ' Add the BONNUMMER column
                Dim bonnummer As DataColumn = table.Columns.Add("BONNUMMER", System.Type.GetType("System.Int32"))
                bonnummer.AllowDBNull = False
    
                ' Add the KOSTENPLAATSNUMMER column
                Dim kostenplaatsnummer As DataColumn = table.Columns.Add("KOSTENPLAATSNUMMER", System.Type.GetType("System.String"))
                kostenplaatsnummer.AllowDBNull = False
                kostenplaatsnummer.MaxLength = 4
    
                ' Add the UREN column
                Dim uren As DataColumn = table.Columns.Add("UREN", System.Type.GetType("System.Int16"))
    
                table.PrimaryKey = New DataColumn() {table.Columns("WERKNEMERNR"), table.Columns("WEEKNUMMER"), table.Columns("DAGNUMMER"), table.Columns("ORDERNUMMER"), table.Columns("BONNUMMER"), table.Columns("KOSTENPLAATSNUMMER")}
    
    
            End Sub
    
            ''' <summary>
            ''' Defines a ForeignKeyConstraint and DataRelation between the "SalesPerson" and "Store" DataTables in a DataSet.
            ''' </summary>
            ''' <param name="storeTrackerDataSet">DataSet in which the constraint and data relation will be defined</param>
            Private Shared Sub DefineConstraintsAndRelations(ByVal storeTrackerDataSet As DataSet)
    
                ' Get the parent column in "SalesPerson" table, and child column in "Store" table
                'Dim parentColumn As DataColumn = DsOrdOvz.BONDataTable
                'Dim childColumn As DataColumn = storeTrackerDataSet.Tables("Store").Columns("SalesPersonID")
    
                ' Define a ForeignKeyConstraint between the two columns
                'Dim constraint As New ForeignKeyConstraint("FK_SalesPerson_Store_SalesPersonID", parentColumn, childColumn)
                'constraint.DeleteRule = Rule.Cascade
                'constraint.UpdateRule = Rule.Cascade
                'constraint.AcceptRejectRule = AcceptRejectRule.Cascade
    
                ' Define a DataRelation between the two columns
                'Dim relation As New DataRelation("SalesPerson_Store_Relation", parentColumn, childColumn)
                'storeTrackerDataSet.Relations.Add(relation)
    
            End Sub
    
    #End Region
    
    #Region "Declarations of data adapters"
    
            Private Shared Function CreatesrvuurDataAdapter() As OleDbDataAdapter
                Dim sql As String
                ' Get a connection object
                Dim connection As OleDbConnection = DataConnection.ConnectionManager.GetConnection()
    
                ' Create a data adapter
                Dim dataAdapter As New OleDbDataAdapter()
                dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
                ' Add the SelectCommand
                sql = "SELECT WERKNEMERNR,WEEKNUMMER,DAGNUMMER,ORDERNUMMER,BONNUMMER,KOSTENPLAATSNUMMER,UREN FROM SRVUUR"
                dataAdapter.SelectCommand = New OleDbCommand(sql, connection)
                dataAdapter.SelectCommand.CommandType = CommandType.Text
    
                ' Add the InsertCommand
                sql = "INSERT INTO SRVUUR (WERKNEMERNR, WEEKNUMMER, DAGNUMMER, ORDERNUMMER, BONNUMMER ,KOSTENPLAATSNUMMER, UREN)" & _
                " VALUES (@prmWERKNEMERNR, @prmWEEKNUMMER, @prmDAGNUMMER, @prmORDERNUMMER, @prmBONNUMMER, @prmKOSTENPLAATSNUMMER, @prmUREN)"
                dataAdapter.InsertCommand = New OleDbCommand(sql, connection)
                dataAdapter.InsertCommand.CommandType = CommandType.Text
                dataAdapter.InsertCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                dataAdapter.InsertCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, 0, "WEEKNUMMER")
                dataAdapter.InsertCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, 0, "DAGNUMMER")
                dataAdapter.InsertCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, 0, "ORDERNUMMER")
                dataAdapter.InsertCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, 0, "BONNUMMER")
                dataAdapter.InsertCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
                dataAdapter.InsertCommand.Parameters.Add("@prmUREN", OleDbType.SmallInt, 0, "UREN")
    
                ' Add the UpdateCommand
                sql = "UPDATE SRVUUR SET WERKNEMERNR=@prmWERKNEMERNR,WEEKNUMMER=@prmWEEKNUMMER,DAGNUMMER=@prmDAGNUMMER" & _
                ", ORDERNUMMER=@prmORDERNUMMER,BONNUMMER=@prmBONNUMMER,KOSTENPLAATSNUMMER=@prmKOSTENPLAATSNUMMER,UREN = @prmUREN WHERE" & _
                " WERKNEMERNR = @prmWERKNEMERNRold AND WEEKNUMMER = @prmWEEKNUMMERold AND DAGNUMMER = @prmDAGNUMMERold AND" & _
                " ORDERNUMMER = @prmORDERNUMMERold AND BONNUMMER = @prmBONNUMMERold AND KOSTENPLAATSNUMMER = @prmKOSTENPLAATSNUMMERold"
                dataAdapter.UpdateCommand = New OleDbCommand(sql, connection)
                dataAdapter.UpdateCommand.CommandType = CommandType.Text
                dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, 0, "WEEKNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, 0, "DAGNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, 0, "ORDERNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, 0, "BONNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmUREN", OleDbType.SmallInt, 0, "UREN")
    
                dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNRold", OleDbType.Char, 6, "WERKNEMERNR").SourceVersion = DataRowVersion.Original
                dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMERold", OleDbType.Integer, 0, "WEEKNUMMER").SourceVersion = DataRowVersion.Original
                dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMERold", OleDbType.SmallInt, 0, "DAGNUMMER").SourceVersion = DataRowVersion.Original
                dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMERold", OleDbType.Integer, 0, "ORDERNUMMER").SourceVersion = DataRowVersion.Original
                dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMERold", OleDbType.Integer, 0, "BONNUMMER").SourceVersion = DataRowVersion.Original
                dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMERold", OleDbType.Char, 4, "KOSTENPLAATSNUMMER").SourceVersion = DataRowVersion.Original
    
                ' Add the DeleteCommand
                sql = "DELETE FROM SRVUUR WHERE WERKNEMERNR = @prmWERKNEMERNR AND WEEKNUMMER = @prmWEEKNUMMER AND DAGNUMMER = @prmDAGNUMMER AND" & _
                " ORDERNUMMER = @prmORDERNUMMER AND BONNUMMER = @prmBONNUMMER AND KOSTENPLAATSNUMMER = @prmKOSTENPLAATSNUMMER"
                dataAdapter.DeleteCommand = New OleDbCommand(sql, connection)
                'dataAdapter.DeleteCommand.CommandType = CommandType.Text
                dataAdapter.UpdateCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.Char, 6, "WERKNEMERNR")
                dataAdapter.UpdateCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.Integer, 0, "WEEKNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.SmallInt, 0, "DAGNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.Integer, 0, "ORDERNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmBONNUMMER", OleDbType.Integer, 0, "BONNUMMER")
                dataAdapter.UpdateCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.Char, 4, "KOSTENPLAATSNUMMER")
    
                ' Return the data adapter
                Return dataAdapter
    
            End Function
    
    
    #End Region
    
    #Region "Methods to get a populated DataSet, and to save a DataSet"
    
            ''' <summary>
            ''' Returns a DataSet containing SalesPerson and Store data.
            ''' </summary>
            ''' <returns>DataSet object</returns>
            Public Shared Function GetData() As DataSet
    
                ' Create a DataSet object
                Dim dsSRVUUR As DataSet = CreateSRVUURDataSet()
    
                ' Fill the "SalesPerson" and "Store" DataTables
                'dsSRVUUR.EnforceConstraints = False
                Try
                    srvuurDataAdapter.Fill(dsSRVUUR.Tables("SRVUUR"))
                    'dsSRVUUR.EnforceConstraints = True
                Catch ex As Exception
                    MessageBox.Show(ex.StackTrace)
                    MessageBox.Show(ex.Message)
                    MessageBox.Show(ex.Source)
                End Try
    
    
                ' Return the DataSet
                Return dsSRVUUR
    
            End Function
    
            ''' <summary>
            ''' Saves DataSet data to the database. Merges data updates (from the database) back into the DataSet.
            ''' </summary>
            ''' <param name="changesDataSet">DataSet object</param>
            Public Shared Sub SaveData(ByRef changesDataSet As DataSet)
    
                'Dim addedDataSet As DataSet = changesDataSet.GetChanges(DataRowState.Added)
                'If Not (addedDataSet Is Nothing) Then
    
                'srvuurDataAdapter.Update(addedDataSet.Tables("SRVUUR"))
                'changesDataSet.Merge(addedDataSet)
    
                'End If
    
                ' Save the Added rows (if any) in the "SalesPerson" and "Store" DataTables, and merge-in data from the database
                ' Save the Modified rows (if any) in the "SalesPerson" and "Store" DataTables, and merge-in data from the database
                Try
                    Dim modifiedDataSet As DataSet = changesDataSet.GetChanges(DataRowState.Modified)
                    If Not (modifiedDataSet Is Nothing) Then
    
                        srvuurDataAdapter.Update(modifiedDataSet.Tables("SRVUUR"))
                        'changesDataSet.Merge(modifiedDataSet)
    
                    End If
                Catch exform As FormatException
                    MessageBox.Show(exform.Source)
                    MessageBox.Show(exform.Message)
                Catch ex As Exception
                    MessageBox.Show(ex.StackTrace)
                    MessageBox.Show(ex.Message)
                    MessageBox.Show(ex.Source)
                End Try
    
    
    
                ' Save the Deleted rows (if any) in the "Store" and "SalesPerson" DataTables, and merge-in data from the database
                Dim deletedDataSet As DataSet = changesDataSet.GetChanges(DataRowState.Deleted)
                If Not (deletedDataSet Is Nothing) Then
    
                    srvuurDataAdapter.Update(deletedDataSet.Tables("SRVUUR"))
                    changesDataSet.Merge(deletedDataSet)
    
                End If
    
            End Sub
    
    #End Region
    
        End Class
    
    End Namespace
    
     
    Imports System.Configuration
    Imports System.Data.OleDb
    Imports System.Data.Common
    Imports Pervasive.Data.SqlClient
    Namespace DataConnection
    
        Friend NotInheritable Class ConnectionManager
    
            Public Shared Function GetConnection() As OleDbConnection
    
                'Dim connectionstring As String = "Database=Europe;Server=hrp-dc1;Port=1583"
                Dim connectionstring As String = ConfigurationManager.ConnectionStrings("ORDSRV2008.My.MySettings.ConnectionString").ConnectionString
                Dim conn As New OleDbConnection(connectionstring)
    
                Return conn
    
            End Function
    
        End Class
    
    End Namespace
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="ORDSRV2008.My.MySettings.ConnectionString" connectionString="Provider=PervasiveOLEDB.;Data Source=TEST;Location=HRP-DC1;Initial Catalog=TEST"
                providerName="Provider=system.data.OLEDB" />
        </connectionStrings>
        <system.diagnostics>
            <sources>
                <!-- This section defines the logging configuration for My.Application.Log -->
                <source name="DefaultSource" switchName="DefaultSwitch">
                    <listeners>
                        <add name="FileLog"/>
                        <!-- Uncomment the below section to write to the Application Event Log -->
                        <!--<add name="EventLog"/>-->
                    </listeners>
                </source>
            </sources>
            <switches>
                <add name="DefaultSwitch" value="Information" />
            </switches>
            <sharedListeners>
                <add name="FileLog"
                     type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                     initializeData="FileLogWriter"/>
                <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
                <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
            </sharedListeners>
        </system.diagnostics>
      <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="myAssembly"
                              publicKeyToken="32ab4ba45e0a69a1"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    {"Poging tot het lezen of schrijven van beveiligd geheugen. Dit duidt er vaak op dat ander geheugen is beschadigd."}
    {System.Collections.ListDictionaryInternal}

    "   bij System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)    bij System.Data.OleDb.OleDbCommand.ExecuteCommandTextForMultpleResults(tagDBPARAMS dbParams, Object& executeResult)    bij System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)    bij System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)    bij System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)    bij System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)    bij System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)    bij System.Data.Common.DbDataAdapter.UpdateRowExecute(RowUpdatedEventArgs rowUpdatedEvent, IDbCommand dataCommand, StatementType cmdIndex)    bij System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)    bij System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)    bij System.Data.Common.DbDataAdapter.Update(DataTable dataTable)    bij ORDSRV2008.DataAcces.SRVUURDAC.SaveData(DataSet& changesDataSet) in C:\Documents and Settings\ivo\Mijn documenten\Visual Studio 2008\Projects\ORDSRV2008\ORDSRV2008\DataAcces.vb:regel 224"

    {"Methode mag alleen worden aangeroepen voor een type waarvoor Type.IsGenericParameter true is."}
    Translation: This methode can only be called when it is a Type.IsGenericParameter true is

    Hope this will trigger your knowledge base.

    Best regards,
    Ivo Keereweer.
    Friday, January 29, 2010 1:43 PM
  • Hi Keereweer,  

    Sorry for late reply, and thank you for your post. I will continue to analyze your codes and try to reproduce the error.

    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, February 3, 2010 8:00 AM
    Moderator
  • Hi Alex,

    I hope you will find the missing piece, cause I'm puzzled...
    I have e remark on code : Dim frmUUR As New frmUren(Me.DsOrdOvz, SRVUURrow)
    This I have already changed to Dim frmUUR As New frmUren(Me.DsSRVUUR, SRVUURrow). Then you know that this is not the case.
    Wednesday, February 3, 2010 8:23 AM
  • Hi Alex,

    So as we stated before it must have something to do with the settings of the adapter.
    I have been doing other things lately. Today I thought to give it a shot. Suddenly my eyes opened and noticed de Deletecommand.
    It's updating the Update command!. I corrected the statements.
    Now the update is doing perfect. You must have noticed this typical copy move.

    dataAdapter.DeleteCommand.Parameters.Add("@prmWERKNEMERNR", OleDbType.VarChar, 6, "WERKNEMERNR")
                dataAdapter.DeleteCommand.Parameters.Add("@prmWEEKNUMMER", OleDbType.[Integer], 10, "WEEKNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmDAGNUMMER", OleDbType.[Integer], 5, "DAGNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmORDERNUMMER", OleDbType.[Integer], 10, "ORDERNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmBONNUMMER", OleDbType.[Integer], 10, "BONNUMMER")
                dataAdapter.DeleteCommand.Parameters.Add("@prmKOSTENPLAATSNUMMER", OleDbType.VarChar, 4, "KOSTENPLAATSNUMMER")

    Thanx a lot for your comment.
    • Marked as answer by Keereweer Tuesday, March 2, 2010 4:08 PM
    Tuesday, March 2, 2010 4:08 PM