|
-
Mar 31st, 2011, 11:38 AM
#1
Thread Starter
Addicted Member
DataRelation Problem
i try to create DataRelation and get error
here my code:
vb.net Code:
Public Class Form1 Dim ds As DataSet Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds = New DataSet Dim dt As New DataTable("dtCustomer") Dim fid As New DataColumn("id", System.Type.GetType("System.Int32")) Dim fkd As New DataColumn("kode", System.Type.GetType("System.String")) Dim fnm As New DataColumn("Name", System.Type.GetType("System.String")) Dim fkel As New DataColumn("idkel", System.Type.GetType("System.Int32")) ' field properties fid.AllowDBNull = False fid.AutoIncrement = True fid.AutoIncrementStep = 2 fkd.AllowDBNull = False fkd.MaxLength = 5 fnm.AllowDBNull = False fnm.MaxLength = 50 fkel.AllowDBNull = False dt.Columns.Add(fid) dt.Columns.Add(fkd) dt.Columns.Add(fnm) dt.Columns.Add(fkel) 'create primary Dim uc As UniqueConstraint = New UniqueConstraint(fid) dt.Constraints.Add(uc) uc = New UniqueConstraint(fkd) dt.Constraints.Add(uc) Dim dtkey(2) As DataColumn dtkey(0) = dt.Columns("id") dtkey(1) = dt.Columns("kode") dt.PrimaryKey = dtkey Dim dr As DataRow For i As Integer = 1 To 20 dr = dt.NewRow dr.Item("kode") = "A00" & i dr.Item("Name") = "SAMPLE KE " & i If i Mod 4 = 0 Then dr("idkel") = 1 ElseIf i Mod 3 = 0 Then dr("idkel") = 3 Else dr("idkel") = 2 End If dt.Rows.Add(dr) Next ds.Tables.Add(dt) Dim fkid As New DataColumn("id", System.Type.GetType("System.Int32")) Dim fknm As New DataColumn("nmkel", System.Type.GetType("System.String")) Dim dt2 As New DataTable("dtkelompok") fkid.AutoIncrement = True fkid.AllowDBNull = False fknm.AllowDBNull = False fknm.MaxLength = 50 dt2.Columns.Add(fkid) dt2.Columns.Add(fknm) Dim unik As UniqueConstraint = New UniqueConstraint(fkid) Dim pkk(1) As DataColumn pkk(0) = dt2.Columns("id") dt2.PrimaryKey = pkk Dim row As DataRow For i As Integer = 1 To 3 row = dt2.NewRow row("nmkel") = "Category " & i dt2.Rows.Add(row) Next ds.Tables.Add(dt2) 'relation Dim dtRelation As New DataRelation("qcustomer", fkel, fkid, True) ds.Relations.Add(dtRelation) End Sub End Class
i get error at line
Code:
ds.Relations.Add(dtRelation)
error message :
Code:
These columns don't currently have unique values.
-
Mar 31st, 2011, 11:56 AM
#2
Re: DataRelation Problem
It looks like you have your parent and child column names backwards.
My usual boring signature: Nothing
 
-
Mar 31st, 2011, 11:59 AM
#3
Thread Starter
Addicted Member
Re: DataRelation Problem
keep how is the solution?
If the code that I create properly?
-
Mar 31st, 2011, 12:01 PM
#4
Re: DataRelation Problem
try this:
vb Code:
Dim dtRelation As New DataRelation("qcustomer", ds.Tables("dtCustomer").Columns(fkel), ds.Tables("dtkelompok").Columns(fkid), True)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2011, 12:10 PM
#5
Thread Starter
Addicted Member
Re: DataRelation Problem
Thank you,
I try but still have not managed
-
Mar 31st, 2011, 12:21 PM
#6
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
 
-
Mar 31st, 2011, 11:31 PM
#7
Thread Starter
Addicted Member
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)
-
Mar 31st, 2011, 11:45 PM
#8
Thread Starter
Addicted Member
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?
-
Mar 31st, 2011, 11:46 PM
#9
Thread Starter
Addicted Member
Re: DataRelation Problem
i replace code be :
vb Code:
For i As Integer = 1 To 20
dr = dt.NewRow
dr.Item("kode") = "A00" & i
dr.Item("Name") = "SAMPLE KE " & i
If i Mod 4 = 0 Then
dr("idkel") = 0
ElseIf i Mod 3 = 0 Then
dr("idkel") = 1
Else
dr("idkel") = 2
End If
dt.Rows.Add(dr)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|