Solved

Getting Relational Data from dataset and fill to dropdown

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

17 Experts available now in Live!

Get 1:1 Help Now