|
-
Jun 8th, 2003, 10:43 AM
#1
Thread Starter
Sleep mode
Relations + ADO.NET ?[Resolved]
I'm getting this error that says : "This constraint cannot be enabled as not all values have corresponding parent values "
I'm getting data from two relational tables . Hmm , have a look at the code .
How can I solve this problem ?
Code:
public void ShowInGrid()
{
DataSet ds;
//two commands for MainCat_Tab & SubCat_Tab tables
System.Data.OleDb.OleDbCommand cmd;
System.Data.OleDb.OleDbCommand cmd2;
//two adapters for filling ds obj
System.Data.OleDb.OleDbDataAdapter adp;
System.Data.OleDb.OleDbDataAdapter adp2;
//this handles the relationship between
//the two columns
System.Data.DataRelation datarelation;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
ds =new DataSet();
ds.CaseSensitive=true;
//First command for first table
cmd=new OleDbCommand ();
cmd.Connection=DBSpace.FunctionClass.MyConnection;
cmd.CommandText="SELECT * FROM MainCat_Tab";
//Second command for Second table
cmd2=new OleDbCommand ();
cmd2.Connection=DBSpace.FunctionClass.MyConnection;
cmd2.CommandText="SELECT * FROM SubCat_Tab";
//first table
adp=new OleDbDataAdapter ();
adp.SelectCommand =cmd;
adp.TableMappings.Add("Table","MainCat_Tab");
adp.Fill(ds);
//Second table
adp2=new OleDbDataAdapter ();
adp2.SelectCommand=cmd2;
adp2.TableMappings.Add ("Table","SubCat_Tab");
adp2.Fill(ds);
dc1=ds.Tables["MainCat_Tab"].Columns["MainCat_ID"];
dc2=ds.Tables["SubCat_Tab"].Columns["SubCat_ID"];
datarelation=new System.Data.DataRelation("MainCat_TabToSubCat_Tab",dc1,dc2);
ds.Relations.Add (datarelation);
Here is the error dataGrid1.DataSource=ds;//.DefaultViewManager;
dataGrid1.DataMember="MainCat_Tab";
}
Last edited by Pirate; Jun 8th, 2003 at 05:12 PM.
-
Jun 8th, 2003, 12:35 PM
#2
Frenzied Member
I dont have any idea what the problem might, since I have'nt really worked with DataRelations. You might wanna try it with typed DataSets.
-
Jun 8th, 2003, 12:52 PM
#3
Thread Starter
Sleep mode
Originally posted by DevGrp
I dont have any idea what the problem might, since I have'nt really worked with DataRelations. You might wanna try it with typed DataSets.
I have no idea about this thing , . Do you think this might help solving my problem ?
-
Jun 8th, 2003, 12:57 PM
#4
Frenzied Member
It might help. I'll write a small demo when I get home. BTW what does your table relationship look like? Also what exactly are you trying to accomplish?
-
Jun 8th, 2003, 01:02 PM
#5
Thread Starter
Sleep mode
-
Jun 8th, 2003, 04:53 PM
#6
Frenzied Member
Do you want each table bound to a different datagrid?
-
Jun 8th, 2003, 04:58 PM
#7
Thread Starter
Sleep mode
Originally posted by DevGrp
Do you want each table bound to a different datagrid?
NO , I'm following the same example written in a book here . I wrote the code , it showed two tables in one DataGrid. My code seems fine , but ....
-
Jun 8th, 2003, 05:02 PM
#8
Frenzied Member
Originally posted by Pirate
NO , I'm following the same example written in a book here . I wrote the code , it showed two tables in one DataGrid. My code seems fine , but ....
I dont understand. Why dont you post the project and I'll take a look at it.
-
Jun 8th, 2003, 05:06 PM
#9
Thread Starter
Sleep mode
The whole proj is the code I posted at the top (all of it) . I don't have other code though (the database is full now with 200 records ) .
-
Jun 8th, 2003, 05:11 PM
#10
Thread Starter
Sleep mode
Yeeeeeeees , I did it . You know what was the problem . I had to map the same column because there is a relation between them (not with SubCat_ID) . hehe , very simple , like this :
Code:
dc1=ds.Tables["MainCat_Tab"].Columns["MainCat_ID"];
dc2=ds.Tables["SubCat_Tab"].Columns["MainCat_ID"];
Thanks DevGrp for trying to help .
-
Jun 8th, 2003, 05:22 PM
#11
Frenzied Member
Glad to here that you found the problem.
Happy coding.
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
|