J
John
Hi
I have a simple data entry form with a single datagrid. Records are added and delete just fine. The problem is that when I try to modify an existing row and press the update button, I get this error;
"An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll
Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
The underlying access table contains only one column 'type' which is also the primary key.
Would appreciate any help in fixing this. The entire code is given below with the offending line highlighted in red.
Thanks
Regards
= Code Follows ==========================================
Public Class frmVenueTypes
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'Attempt to update the datasource.
Me.UpdateDataSet()
Me.Close()
End Sub
Public Sub UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the main dataset.
Dim objDataSetChanges As Contacts.dsContacts = New Contacts.dsContacts
'Stop any current edits.
Me.BindingContext(objdsSetup, "tblvTypes").EndCurrentEdit()
'Get the changes that have been made to the main dataset.
objDataSetChanges = CType(objdsSetup.GetChanges, Contacts.dsContacts)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
'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)
objdsSetup.Merge(objDataSetChanges) ' <== Error comes on this line
objdsSetup.AcceptChanges()
'Add your code to check the returned dataset for any errors that may have been
'pushed into the row object's error.
End If
End Sub
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 Contacts.dsContacts
objDataSetTemp = New Contacts.dsContacts
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
grdtblvStatus.DataSource = Nothing
'Empty the old records from the dataset.
objdsSetup.Clear()
'Merge the records into the main dataset.
objdsSetup.Merge(objDataSetTemp)
grdtblvStatus.SetDataBinding(objdsSetup, "tblvTypes")
Catch eLoadMerge As System.Exception
'Add your error handling code here.
Throw eLoadMerge
End Try
End Sub
Public Sub UpdateDataSource(ByVal ChangedRows As Contacts.dsContacts)
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)
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 Contacts.dsContacts)
'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)
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
Private Sub frmVenueTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Attempt to load the dataset.
Me.LoadDataSet()
Catch eLoad As System.Exception
'Add your error handling code here.
'Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
End Sub
End Class
I have a simple data entry form with a single datagrid. Records are added and delete just fine. The problem is that when I try to modify an existing row and press the update button, I get this error;
"An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll
Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
The underlying access table contains only one column 'type' which is also the primary key.
Would appreciate any help in fixing this. The entire code is given below with the offending line highlighted in red.
Thanks
Regards
= Code Follows ==========================================
Public Class frmVenueTypes
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'Attempt to update the datasource.
Me.UpdateDataSet()
Me.Close()
End Sub
Public Sub UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the main dataset.
Dim objDataSetChanges As Contacts.dsContacts = New Contacts.dsContacts
'Stop any current edits.
Me.BindingContext(objdsSetup, "tblvTypes").EndCurrentEdit()
'Get the changes that have been made to the main dataset.
objDataSetChanges = CType(objdsSetup.GetChanges, Contacts.dsContacts)
'Check to see if any changes have been made.
If (Not (objDataSetChanges) Is Nothing) Then
'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)
objdsSetup.Merge(objDataSetChanges) ' <== Error comes on this line
objdsSetup.AcceptChanges()
'Add your code to check the returned dataset for any errors that may have been
'pushed into the row object's error.
End If
End Sub
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 Contacts.dsContacts
objDataSetTemp = New Contacts.dsContacts
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
grdtblvStatus.DataSource = Nothing
'Empty the old records from the dataset.
objdsSetup.Clear()
'Merge the records into the main dataset.
objdsSetup.Merge(objDataSetTemp)
grdtblvStatus.SetDataBinding(objdsSetup, "tblvTypes")
Catch eLoadMerge As System.Exception
'Add your error handling code here.
Throw eLoadMerge
End Try
End Sub
Public Sub UpdateDataSource(ByVal ChangedRows As Contacts.dsContacts)
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)
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 Contacts.dsContacts)
'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)
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
Private Sub frmVenueTypes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Attempt to load the dataset.
Me.LoadDataSet()
Catch eLoad As System.Exception
'Add your error handling code here.
'Display error message, if any.
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
End Sub
End Class