Solved

Getting Relational Data from dataset and fill to dropdown

Posted on 2007-11-20
3
969 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now