Relation between Data Tables

vks.gautam1

Well-known member
Joined
Oct 10, 2008
Messages
78
Location
Chandigarh, India
Programming Experience
Beginner
Here i have two Tables --- " Mef_Info " , "Mef_Detail.

Pack_no is same field in both tables
i fill both the tables in Datatables.
& want to fill datagridview.

but it is not working.

But getting this error-
contactswitchdeadlock was detected.

" The CLR has been unable to transition from COM context 0x1a0c48 to COM context 0x1a0db8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations. "


VB.NET:
Public Class frmDatasetRelation
    Dim cn As OleDbConnection
    Dim cmd As String
    Dim ds As DataSet
     Dim ad As OleDbDataAdapter
    Private Sub btnDatTableRelation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDatTableRelation.Click
        Try
                    
           
            cn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=Data source path;")
            cn.Open()
            cmd = "select * from mef_info where khattar_dt>#11/14/2008#"
            ds = New DataSet
            ad = New OleDbDataAdapter(cmd, cn)
            ad.Fill(ds, "mef_info")
            cmd = "select * from mef_detail"
            ad = New OleDbDataAdapter(cmd, cn)
            ad.Fill(ds, "mef_detail")
            Dim relation As New DataRelation("Mef", ds.Tables("Mef_Info").Columns("Pack_no"), ds.Tables("Mef_Detail").Columns("Pack_no"))
            ds.Relations.Add(relation)
            Dim dv As New DataView(ds.Tables("mef_Info"))
            DataGridView1.DataSource = dv


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    
End Class
 
First up, why are opening your connection and never closing it? If you're going to open it explicitly then close it explicitly too. If you're not going to close it explicitly then don't open it explicitly.

Secondly, why have you declared all your variables at the class level if they're only being used in that method?

Thirdly, while it should not theoretically be an issue it's certainly bad practice to use that same DataAdapter variable for two different DataAdapters. It simply serves to make the code less clear, which is almost always a bad thing.

Finally, what exactly is your DataRelation for? If you want to be able to filter the child data by parent then you should be using BindingSources and binding rather differently. Take a look at this.

Fix all those things and then let's see if your problem remains.
 
Actually, use the dataset designer to make a typed dataset and tableadapters for this, and the problem will not appear in the first place :)
 
Back
Top