JaedenRuiner
Well-known member
- Joined
- Aug 13, 2007
- Messages
- 340
- Programming Experience
- 10+
Well,
I've done a lot of revamping, reducing the global access to everything, and made the application a bit more modular overall. But a weird problem that I don't quite see how it could be occurring keeps popping up.
As with all typed datasets generated by the wizard/designer it comes with the basic TableAdapterManager, MyDatabaseDataSet, and all the typed DataTables and TableAdapters for the database. Simple.
I Extended this to bascially provide a link between each DataTable and their respective TableAdapters, as well as I provided a Refresh command on all these DataTables which intrinsically calls it's own Adapter's Fill() method.
I continued to take the two primary classed (TableAdapterManager and MyDatabaseDataSet) and wrapped them into another class that controls and manipulates everything I would ever possibly need with the typed dataset, and I must say it makes life easier.
My FillDataSet is also recursive, basically, reading the table it is given to fill, and if that table happens to have any parentrelations, it fills them first to maintain relational integrity as with the commit.
So my problem:
I load my app, Log into the Database, and open my Database "Form" (the one which directly manipulates all the tables and such).
The first time i load it up, the form becomes visible and all is hunky dory. I close the form.
As you can see i've had to set it up to work with the closing of the form, so that the form would be hidden during application operation, but when Me.Hide() is executed the Visible_Changed event triggers and the connection and dataset is cleared (disposed)
Then to test I re-open the form, and thats when I get a ConstraintException:
Now, i wouldn't have too much concern, because I know such things can happen, but It loaded great the first time. but no matter how many time I execute disconnect/connect in my app, it won't load past the a specific table. What I'm concerned about mostly, is that the data is in the database and it doesn't seem to have a problem with it, and the fact that the dataset loads fine the first time is very suspect.
below is my FillDataset base code, to help follow the flow of what is going on in there, but overall, i'm a bit stymied.
Thanks
I've done a lot of revamping, reducing the global access to everything, and made the application a bit more modular overall. But a weird problem that I don't quite see how it could be occurring keeps popping up.
As with all typed datasets generated by the wizard/designer it comes with the basic TableAdapterManager, MyDatabaseDataSet, and all the typed DataTables and TableAdapters for the database. Simple.
I Extended this to bascially provide a link between each DataTable and their respective TableAdapters, as well as I provided a Refresh command on all these DataTables which intrinsically calls it's own Adapter's Fill() method.
I continued to take the two primary classed (TableAdapterManager and MyDatabaseDataSet) and wrapped them into another class that controls and manipulates everything I would ever possibly need with the typed dataset, and I must say it makes life easier.
- MyWrapper.Commit() - Cycles through all tables and attempts to Update() any added, modified, or deleted rows:
- CommitUpd() is recursive, cycling through ParentRelations collection and Commiting any Parent Tables First.
- CommitDel() is Recursive, cycling through ChildRelations collections and committing and Child Tables First
- MyWrapper.FillDataset() - Fills all the typed tables of the dataset, (including my views one of which is a list and typing flag of each table in my database. This is for usage determinations in my app, and quite helpful overall).
My FillDataSet is also recursive, basically, reading the table it is given to fill, and if that table happens to have any parentrelations, it fills them first to maintain relational integrity as with the commit.
So my problem:
I load my app, Log into the Database, and open my Database "Form" (the one which directly manipulates all the tables and such).
VB.NET:
Private _RexCon as MyWrapper ' It's not named that, but i'm saving on typing
Private Sub AdminForm_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.VisibleChanged
If Me.Visible Then
'If A User/Pass is not supplied
If (Not My.Application.ConnectionMgr.CanConnect) Then
My.Application.DoLogon(Me, _RexCon) ' Open the "Login" form and attempt connect
Else 'otherwise User/Pass is available so just connect directly
_RexCon = New RexamDatabase.RexamDBConnection(My.Application.ConnectionMgr)
_RexCon.Connect(My.Application.UserLogin)
End If
If _RexCon IsNot Nothing AndAlso _RexCon.Connected Then
InitBindings() 'Form Method added to create ALL databindings for
' controls, binding sources, and DataGridViews
_RexCon.FillDataset() 'Fills the Dataset
Me.TabCtrl.Enabled = _RexCon.DSFilled 'Enables controls
WasHidden = False
FormTimer.Enabled = True
End If
Else
DisconnectBindings() 'Disconnects ALL databindings for controls,
'bindingsources, and DataGridViews
_RexCon.Dispose() 'Disposes all of the object implementing IDisposable,
'Closing internal SqlConnections, and clearing the dataset
_RexCon = Nothing
FormTimer.Enabled = False
WasHidden = True
End If
End Sub
The first time i load it up, the form becomes visible and all is hunky dory. I close the form.
VB.NET:
Private Sub AdminForm_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
Me.Hide()
End If
If My.Application.Preferences.SaveLayout Then
My.Settings.AdminLoc = Me.Location
My.Settings.AdminSize = Me.Size
End If
End Sub
Then to test I re-open the form, and thats when I get a ConstraintException:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Now, i wouldn't have too much concern, because I know such things can happen, but It loaded great the first time. but no matter how many time I execute disconnect/connect in my app, it won't load past the a specific table. What I'm concerned about mostly, is that the data is in the database and it doesn't seem to have a problem with it, and the fact that the dataset loads fine the first time is very suspect.
below is my FillDataset base code, to help follow the flow of what is going on in there, but overall, i'm a bit stymied.
VB.NET:
''' <summary>
''' Fills the Entire RexamShipDataSet Datatables.
''' </summary>
''' <returns>Boolean - Whether or not the dataset was filled.</returns>
''' <remarks></remarks>
Public Function FillDataset() As Boolean
If Connected Then
_filling = True
_dataset.Clear()
RefreshTableList()
For Each tbl As DataTable In _dataset.Tables
If Not Refresh(tbl) Then 'function below
Return False
End If
Next
_filling = False
_dsFilled = True
RaiseEvent DataSetFilled(Me, New EventArgs())
Return True
End If
Return False
End Function
Protected Overloads Function Refresh(ByVal tbl As DataTable, _
Optional ByVal bRefill As Boolean = False) As Boolean
Dim b As Boolean = False
If Connected AndAlso (tbl IsNot Nothing) AndAlso _
(bRefill OrElse (Not _dataset.TableInfo(tbl.TableName).Filled)) Then
DoWaitCursor(True)
b = True
For Each rel As DataRelation In tbl.ParentRelations
b = b AndAlso Refresh(rel.ParentTable) 'recurse into this again
Next
b = b AndAlso _dataset.Refresh(tbl) 'function below
If b Then
RaiseRefreshEvent(tbl, _dataset.TableInfo(tbl.TableName).Type)
End If
DoWaitCursor(False)
ElseIf _dataset.TableInfo(tbl.TableName).Filled Then
b = True
End If
Return b
End Function
Public Function Refresh(ByVal tbl As DataTable)
If tbl IsNot Nothing Then
Try
'ta_* prefix is typed TableAdapter
'_manager is internal to the Typed DataSet pointing to the AdapterManager
Select Case tbl.TableName
Case "tbl_BOL"
'this is the table that bombs, even though both parent's are loaded fine
_manager.ta_tblBOL.Fill(tbl_BOL)
Case "tbl_Email"
_manager.ta_tblEmail.Fill(tbl_Email) 'parent to tbl_BOL
Case "tbl_MailBox"
_manager.ta_tblMailBox.Fill(tbl_MailBox) 'parent to tbl_Email
Case "lst_Customers"
_manager.ta_lstCustomers.Fill(lst_Customers)
Case "lst_Material"
_manager.ta_lstMaterial.Fill(lst_Material)
Case "lst_CorbiLoc"
_manager.ta_lstCorbiLoc.Fill(lst_CorbiLoc)
Case "qry_EmailSum"
_manager.ta_qryEmailSum.Fill(qry_EmailSum)
Case "qry_Tables"
RefreshList()
Case Else 'In case it doesn't have a typed adapter for the table
_manager.SpareAdapter.SelectCommand = New SqlClient.SqlCommand( _
Format(tbl.TableName, "SELECT * FROM {0}"), _manager.Connection)
_manager.SpareAdapter.Fill(tbl)
End Select
If Not _list.Contains(tbl.TableName) Then
_list.Add(tbl.TableName, New RexamTableInfo(RexamTableType.View, True, False))
Else
_list(tbl.TableName).Filled = True
End If
_list(tbl.TableName).Filled = True
Return True
Catch ex As Exception
My.Application.LogError(ex, "{0}")
End Try
End If
Return False
End Function
Thanks