Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This constraint cannot be enabled as not all values have corresponding parent values

I have three nested tables in a dataset. I display the data based on the language ID, ie: EN is 1 FR is 2 and NL is 3. French and english exist in the database but Dutch does not exist yet and when the user selects NL I get the following error:

This constraint cannot be enabled as not all values have corresponding parent values.

Below is the code that I use to get the data. The error happens when I try to create relations in the dataset.

 (ds.Relations.Add(new DataRelation("Cat_SubCat", dk1, dk2));

Now my question is, how can I check if the value exists in the dataset or database with the given code below?

 public static DataTable GetData(Int32 languageID)
    {
        DataSet ds = new DataSet();

        string commandText = @"SELECT * FROM AlacarteCat where languageID = @ID;
                               SELECT * FROM AlacarteSubCat where languageID = @ID;
                               SELECT * from AlacarteItems where languageID = @ID";

        using (SqlConnection myConnection = new SqlConnection(Common.GetConnectionString("SQLConnectionString")))
        {
           SqlCommand command = new SqlCommand(commandText, myConnection);
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = languageID;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = command;
            da.TableMappings.Add("AlacarteCat", "AlacarteCat"); // table 0
            da.TableMappings.Add("AlacarteSubCat", "AlacarteSubCat"); // table 1
            da.TableMappings.Add("AlacarteItems", "AlacarteItems"); // table 2
            da.Fill(ds, "AlacarteCat");

            DataColumn dk1 = ds.Tables[0].Columns["ID"];
            DataColumn dk2 = ds.Tables[1].Columns["AlacarteCatID"];
            DataColumn dk3 = ds.Tables[1].Columns["ID"];
            DataColumn dk4 = ds.Tables[2].Columns["AlacarteSubCatID"];
            DataColumn dk5 = ds.Tables[0].Columns["id"];
            DataColumn dk6 = ds.Tables[2].Columns["AlacarteCatID"];
            ds.Relations.Add(new DataRelation("Cat_SubCat", dk1, dk2));
            ds.Relations.Add(new DataRelation("SubCat_Items", dk3, dk4));
            ds.Relations.Add(new DataRelation("Cat_Items", dk5, dk6));

            if ((ds != null))
            {
                return ds.Tables["AlacarteCat"];
            }
            return null;
        }

    }
like image 752
Imir Hoxha Avatar asked May 11 '12 18:05

Imir Hoxha


1 Answers

By default, when you create a relationship, it enforces foreign key constraints, by setting to false, you are telling it that you dont want to enforce the relationship.

ds1.DataRelation.Add(“relCustOrder”, parentCol, childCol, false)

The solutions is to set constraint in DataRelation class to false

like image 178
Niks Avatar answered Oct 19 '22 12:10

Niks