Solved

Getting Relational Data from dataset and fill to dropdown

Posted on 2007-11-20
3
975 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
[X]
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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

738 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