Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone ...
i cannot understand what is wrong with this code if your could please tell me ..
i wanna show detail data in second datagridview with selecte first "master" datagridview

my sql table is

tblA
ID - Name -Clid
1 - john -1
2 -sam -2

tblb

ID - clas
1 - ID
2 - access

and this is my code
C#
DataSet data = new DataSet();
             data.Locale = System.Globalization.CultureInfo.InvariantCulture;

             SqlDataAdapter masterDataAdapter = new
                 SqlDataAdapter("select * from Tbla", connection);
             masterDataAdapter.Fill(data, "Tbla");

             // Add data from the Orders table to the DataSet.
             SqlDataAdapter detailsDataAdapter = new
                 SqlDataAdapter("select * from Tblb", connection);
             detailsDataAdapter.Fill(data, "Tblb");


             DataRelation relation = new DataRelation("CustomersOrders",
                 data.Tables["Tbla"].Columns["Clid "],
                 data.Tables["Tblb"].Columns["ID"]);
             data.Relations.Add(relation);


             dataGridView1.DataMember = "Customers";
             dataGridView1.DataSource = data;

             dataGridView2.DataSource = dataGridView1;
             dataGridView2.DataMember = "CustomersOrders";
         }

and it showing this error{
This constraint cannot be enabled as not all values have corresponding parent values.}

What I have tried:

i tried to change datarelation with first set tblb and then tbla, and tried to remove data DataMember
Posted
Updated 15-Feb-16 8:13am
v2

1 solution

If the data you've posted is the entire content of your tables, then it's no wonder you're getting this error. Where is the parent row in tblA for the first row in tblB? According to the definition of your relationship, there should be a row with Clid = 1, but there isn't.

Correct the data in your database, and then set up a FOREIGN KEY constraint[^] between the tables to prevent this sort of problem from occurring again.

If those are your real table names, then you should strongly consider changing them to something more meaningful - for example, Customers and Orders. It will make your life much easier when you come back to maintain this code later.
 
Share this answer
 
Comments
jame01 15-Feb-16 14:16pm    
thanks for your note sir,
that was just for explain
i tried my data and relation in sql before applay on visualstuido
Richard Deeming 15-Feb-16 14:19pm    
The answer still applies - if you're getting that error, it means there are rows in the child table which do not have corresponding rows in the parent table. Which means either you're using the wrong columns in the DataRelation, or you haven't created a suitable Foreign Key constraint on the tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900