Results 1 to 11 of 11

Thread: Relations + ADO.NET ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ?

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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?

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by DevGrp
    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?
    I want to add two tables with DataRelations and add them to one dataset then to datagrid . Is that clear ?

    Thanks man .
    Attached Images Attached Images  

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Do you want each table bound to a different datagrid?

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ....

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ) .

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
  •  



Click Here to Expand Forum to Full Width