I have three nested tables in a dataset. I show the data based on the language identifier, that is: EN - 1 FR - 2, and NL - 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 restriction cannot be included because not all values have corresponding parent values.
Below is the code that I use to get the data. An error occurs when I try to create relationships in a dataset.
(ds.Relations.Add(new DataRelation("Cat_SubCat", dk1, dk2));
Now my question is: how can I check if a value exists in a 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");
da.TableMappings.Add("AlacarteSubCat", "AlacarteSubCat");
da.TableMappings.Add("AlacarteItems", "AlacarteItems");
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;
}
}
source