Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting Relational Data from dataset and fill to dropdown

Posted on 2007-11-20
3
973 Views
Last Modified: 2013-11-07
Hi,

I am working on C# using .Net 2.0

I have Dataset, In the DataSet oDst, I get 2 tables

I have made the realtion as below

DataSet oDst = oDynmcFld.GetFieldsByFormId("GetFieldsByFormId", 1);
oDst.Relations.Add("ControlValuesRelation", oDst.Tables[0].Columns["FieldId_PK"], oDst.Tables[1].Columns["FormFileds_Mstr_PK"]);


Now I have to create  Dropdown at runtime and add to the html form

I want the Data to be filled in drop down from the relation of the Dataset

As I did in my code like:

            drp = new DropDownList();
                drp.ID = oDt.Rows[iCnt]["FieldID"].ToString();
                drp.DataTextField = "FieldValue";
                drp.DataValueField = "FieldValue";

                drp.DataSource = oDst.Relations["ControlValuesRelation"].ChildTable.DefaultView; //.ChildRelations[oDt.Rows[iCnt]["FormFileds_Mstr_PK"].ToString()]; //.Contains(oDt.Rows[iCnt]["FieldId_PK"].ToString());
                drp.DataBind();
               
But this is filling all the Data present in oDst.Table[1], here I want the data from the Table[1] which matches the relation "ControlValuesRelation"


Please guide

Many Thanks

Here is my code below:



      oDynmcFld = new DynamicFieldBL();
      
        DataSet oDst = oDynmcFld.GetFieldsByFormId("GetFieldsByFormId", 1);
        oDst.Relations.Add("ControlValuesRelation", oDst.Tables[0].Columns["FieldId_PK"], oDst.Tables[1].Columns["FormFileds_Mstr_PK"]);
       
        DataTable oDt = oDst.Tables[0];

       HtmlTableRow htr = new HtmlTableRow();
       HtmlTableCell htc1 = new HtmlTableCell();
       HtmlTableCell htc2 = new HtmlTableCell();
       DropDownList drp = new DropDownList();
       
               
       int startWith = 6;
       

        if (oDt.Rows.Count > 0)
        {
            for (int iCnt = 0; iCnt <= oDt.Rows.Count - 1; iCnt++)
            {
                htr = new HtmlTableRow();
                htc1 = new HtmlTableCell();
                htc2 = new HtmlTableCell();

                sTempHTML += "<tr><td align=\"right\">";
                if (bool.Parse(oDt.Rows[iCnt]["IsMandatory"].ToString()))
                {
                    sTempHTML += "<font class=\"alert\">*</font> ";
                }
               
                drp = new DropDownList();
                drp.ID = oDt.Rows[iCnt]["FieldID"].ToString();
                drp.DataTextField = "FieldValue";
                drp.DataValueField = "FieldValue";

                drp.DataSource = oDst.Relations["ControlValuesRelation"].ChildTable.DefaultView; //.ChildRelations[oDt.Rows[iCnt]["FormFileds_Mstr_PK"].ToString()]; //.Contains(oDt.Rows[iCnt]["FieldId_PK"].ToString());
                drp.DataBind();

                htc2.Controls.Add(drp);

                htr.Cells.Add(htc1);
                htr.Cells.Add(htc2);
                DynamicTable.Rows.Insert(startWith, htr);
// DynamicTable is the Table Id in ASPX file <table width="100%" cellpadding="3" cellspacing="0" border="0" id="DynamicTable" runat="server" >
                startWith = startWith + 1;
              }
         }
oDynmcFld = new DynamicFieldBL();
	
        DataSet oDst = oDynmcFld.GetFieldsByFormId("GetFieldsByFormId", 1);
        oDst.Relations.Add("ControlValuesRelation", oDst.Tables[0].Columns["FieldId_PK"], oDst.Tables[1].Columns["FormFileds_Mstr_PK"]);
        
        DataTable oDt = oDst.Tables[0];
 
       HtmlTableRow htr = new HtmlTableRow();
       HtmlTableCell htc1 = new HtmlTableCell();
       HtmlTableCell htc2 = new HtmlTableCell();
       DropDownList drp = new DropDownList();
       
        
        drp.ID = "newDrp";
        drp.Items.Add(new ListItem("test"));
 
       int startWith = 6;
        
 
        if (oDt.Rows.Count > 0)
        {
            for (int iCnt = 0; iCnt <= oDt.Rows.Count - 1; iCnt++)
            {
                htr = new HtmlTableRow();
                htc1 = new HtmlTableCell();
                htc2 = new HtmlTableCell();
 
                sTempHTML += "<tr><td align=\"right\">";
                if (bool.Parse(oDt.Rows[iCnt]["IsMandatory"].ToString()))
                {
                    sTempHTML += "<font class=\"alert\">*</font> ";
                }
                
                drp = new DropDownList();
                drp.ID = oDt.Rows[iCnt]["FieldID"].ToString();
                drp.DataTextField = "FieldValue";
                drp.DataValueField = "FieldValue";
 
                drp.DataSource = oDst.Relations["ControlValuesRelation"].ChildTable.DefaultView; //.ChildRelations[oDt.Rows[iCnt]["FormFileds_Mstr_PK"].ToString()]; //.Contains(oDt.Rows[iCnt]["FieldId_PK"].ToString());
                drp.DataBind();
 
                htc2.Controls.Add(drp);
 
                htr.Cells.Add(htc1);
                htr.Cells.Add(htc2);
                DynamicTable.Rows.Insert(startWith, htr);
                startWith = startWith + 1;
              }
         }

Open in new window

0
Comment
Question by:tia_kamakshi
  • 2
3 Comments
 
LVL 3

Expert Comment

by:john_steed
ID: 20321772
Hi,

What you need to do is use getChildRows to get the childrows associated with a row in ds1
with drPublisher being one row in dataset1 and drPubsTitles being the relation as so :

drPublisher.GetChildRows(drPubsTitles)

hope this helps

0
 

Author Comment

by:tia_kamakshi
ID: 20321881
Thanks for your reply,

But GetChildRows is the property of DataRow

I do not have DataRow,

What code I should write here
drp = new DropDownList();
                drp.ID = oDt.Rows[iCnt]["FieldID"].ToString();
                drp.DataTextField = "FieldValue";
                drp.DataValueField = "FieldValue";

                drp.DataSource = oDst.Tables[1].getc
                drp.DataBind();

Thanks again
0
 
LVL 3

Accepted Solution

by:
john_steed earned 500 total points
ID: 20322055
the datarelation between table 1 and 2 connects the records from table 1 to table 2 with FormFileds_Mstr_PK = FieldId_PK
so for each row in table 1 you get a few corresponding rows in table 2

do you want to show all records from table 2 that correspond with any row in table 1 ?

in that case you'll have to iterate through it
for each drRow in oDst.tables(0).rows
for each drChildRow in drRows.getChildRows(drRelation)
'add to dropdownlist
next
next
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question