I'm doing connectivity of my sql server database with C sharp code.
In sql server 2005 i made two tables person(empid,citycode,name,salary) and city(name,citycode)and entered few records. i haven't set any constraint on any column.
Person
11 Ashish 101 99000
12 XYZ111 110 993939
13 yuuuuu 101 88484
14 hdhfhh 101 89999
15 hdhdhj 111 88999
City
LosAngels 101
london 110
newyork 111
When i try to fetch these two tables in data set's data tables after setting parent child relationship using data relation i get an error on dataadapter's fill method call "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints". Program works fine on setting enforce constraints to false but i want to know the reason. My records in tables satisfy the primary and foreign key relationship please help me out.
My code is below. Please guide me
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection(@"Data source=ASHOK-268960F0F\ASHISH;initial catalog=db1;integrated security=SSPi");
SqlDataAdapter da = new SqlDataAdapter("select empid,citycode,name,salary from person select name,citycode from city", con);
DataSet ds = new DataSet("Person_City_Detaset");
DataTable dt1 = new DataTable("Person_Table");
DataTable dt2 = new DataTable("City_Table");
ds.Tables.Add(dt1);
dt1.Columns.Add("empid", typeof(int));
dt1.Columns.Add("citycode", typeof(int));
dt1.Columns.Add("name", typeof(string));
dt1.Columns.Add("salary", typeof(double));
ds.Tables.Add(dt2);
dt2.Columns.Add("name", typeof(string));
dt2.Columns.Add("citycode", typeof(int));
//da.Fill (ds);
da.TableMappings.Add("Table", "person_table");
da.TableMappings.Add("Table1", "city_table");
da.Fill(ds);
ds.Relations.Add("Person_City_Relation", dt2.Columns[1], dt1.Columns[1]);
da.Fill(ds); //Error
Console.WriteLine(ds.Tables.Count);
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Table Name-------"+ds.Tables[i].TableName);
foreach (DataColumn col in ds.Tables[i].Columns)
{
Console.WriteLine(col.ColumnName);
}
Console.WriteLine();
}
Console.ReadKey();
}
}