Results 1 to 9 of 9

Thread: DataRelation Problem

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    DataRelation Problem

    i try to create DataRelation and get error

    here my code:
    vb.net Code:
    1. Public Class Form1
    2.     Dim ds As DataSet
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         ds = New DataSet
    6.  
    7.         Dim dt As New DataTable("dtCustomer")
    8.  
    9.         Dim fid As New DataColumn("id", System.Type.GetType("System.Int32"))
    10.         Dim fkd As New DataColumn("kode", System.Type.GetType("System.String"))
    11.         Dim fnm As New DataColumn("Name", System.Type.GetType("System.String"))
    12.         Dim fkel As New DataColumn("idkel", System.Type.GetType("System.Int32"))
    13.  
    14.  
    15.         ' field properties
    16.         fid.AllowDBNull = False
    17.         fid.AutoIncrement = True
    18.         fid.AutoIncrementStep = 2
    19.  
    20.         fkd.AllowDBNull = False
    21.         fkd.MaxLength = 5
    22.  
    23.         fnm.AllowDBNull = False
    24.         fnm.MaxLength = 50
    25.  
    26.         fkel.AllowDBNull = False
    27.  
    28.  
    29.         dt.Columns.Add(fid)
    30.         dt.Columns.Add(fkd)
    31.         dt.Columns.Add(fnm)
    32.         dt.Columns.Add(fkel)
    33.         'create primary
    34.         Dim uc As UniqueConstraint = New UniqueConstraint(fid)
    35.         dt.Constraints.Add(uc)
    36.  
    37.         uc = New UniqueConstraint(fkd)
    38.         dt.Constraints.Add(uc)
    39.  
    40.         Dim dtkey(2) As DataColumn
    41.         dtkey(0) = dt.Columns("id")
    42.         dtkey(1) = dt.Columns("kode")
    43.  
    44.  
    45.         dt.PrimaryKey = dtkey
    46.  
    47.  
    48.  
    49.         Dim dr As DataRow
    50.        
    51.         For i As Integer = 1 To 20
    52.             dr = dt.NewRow
    53.             dr.Item("kode") = "A00" & i
    54.             dr.Item("Name") = "SAMPLE KE " & i
    55.             If i Mod 4 = 0 Then
    56.                 dr("idkel") = 1
    57.             ElseIf i Mod 3 = 0 Then
    58.                 dr("idkel") = 3
    59.             Else
    60.                 dr("idkel") = 2
    61.             End If
    62.             dt.Rows.Add(dr)
    63.         Next
    64.  
    65.         ds.Tables.Add(dt)
    66.  
    67.             Dim fkid As New DataColumn("id", System.Type.GetType("System.Int32"))
    68.         Dim fknm As New DataColumn("nmkel", System.Type.GetType("System.String"))
    69.  
    70.         Dim dt2 As New DataTable("dtkelompok")
    71.  
    72.         fkid.AutoIncrement = True
    73.         fkid.AllowDBNull = False
    74.  
    75.         fknm.AllowDBNull = False
    76.         fknm.MaxLength = 50
    77.  
    78.  
    79.         dt2.Columns.Add(fkid)
    80.         dt2.Columns.Add(fknm)
    81.  
    82.         Dim unik As UniqueConstraint = New UniqueConstraint(fkid)
    83.  
    84.         Dim pkk(1) As DataColumn
    85.  
    86.         pkk(0) = dt2.Columns("id")
    87.         dt2.PrimaryKey = pkk
    88.  
    89.         Dim row As DataRow
    90.         For i As Integer = 1 To 3
    91.             row = dt2.NewRow
    92.             row("nmkel") = "Category " & i
    93.             dt2.Rows.Add(row)
    94.         Next
    95.  
    96.         ds.Tables.Add(dt2)
    97.  
    98.  
    99.         'relation
    100.         Dim dtRelation As New DataRelation("qcustomer", fkel, fkid, True)
    101.         ds.Relations.Add(dtRelation)
    102.     End Sub
    103. End Class
    i get error at line
    Code:
    ds.Relations.Add(dtRelation)
    error message :
    Code:
    These columns don't currently have unique values.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: DataRelation Problem

    It looks like you have your parent and child column names backwards.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DataRelation Problem

    keep how is the solution?

    If the code that I create properly?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: DataRelation Problem

    try this:

    vb Code:
    1. Dim dtRelation As New DataRelation("qcustomer", ds.Tables("dtCustomer").Columns(fkel), ds.Tables("dtkelompok").Columns(fkid), True)

  5. #5

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DataRelation Problem

    Thank you,

    I try but still have not managed

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: DataRelation Problem

    What I was saying was that it looked like fkel is the child, while fkid looks like the parent. After all, fkid is a primary key, while fkel is not. Therefore, it looks like this:

    Dim dtRelation As New DataRelation("qcustomer", fkel, fkid, True)

    should be:

    Dim dtRelation As New DataRelation("qcustomer", fkid, fkel, True)
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DataRelation Problem

    I've tried the way as above but still can not.
    and I get an error message like this:
    Code:
    This constraint cannot be enabled as not all values have corresponding parent values.
    at line
    Code:
    ds.Relations.Add(dtRelation)

  8. #8

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DataRelation Problem

    I've found the location of the fault.
    it turns out there is a value of 0 in field idkel (child) while the value 0 is not found in the parent (id).

    of the above conditions, would not it be if the value 0 is not contained in the parent.
    what if there are conditions like this?

  9. #9

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DataRelation Problem

    i replace code be :
    vb Code:
    1. For i As Integer = 1 To 20
    2.             dr = dt.NewRow
    3.             dr.Item("kode") = "A00" & i
    4.             dr.Item("Name") = "SAMPLE KE " & i
    5.             If i Mod 4 = 0 Then
    6.                 dr("idkel") = 0
    7.             ElseIf i Mod 3 = 0 Then
    8.                 dr("idkel") = 1
    9.             Else
    10.                 dr("idkel") = 2
    11.             End If
    12.             dt.Rows.Add(dr)
    13.         Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width