Solved

Generate Dropdownlists dynamically and bind data from datatable or dataadapter methods

Posted on 2009-07-07
20
345 Views
Last Modified: 2012-05-07
Hi,
     I'm  not much experienced with asp.net development.  I have a database for a kind of quiz application, answers to which are then processed through logic to generate or determine a plan for the user.

I have two tables in the database 1)Questions  table with Qid(P key), Q description etc.

2)QuestionAns table which contains all the answer options for every question in the question table.   Columns are QueAnsId(P. Key),Qid(foreign key), Answers(all answers options for every question).

I created two datasets for each of these tables using the configuration wizards. 1)QuesDS,
2)AnsDS

On their respective tableAdapters, i have written methods GetQuestions(), GetAnswersByQuesId(Qid)containg respective sql queries.

What i want to achieve is,  for each question  displayed , a dropdownlist with all the answer options for that question be displayed. Then a question again and A dropdownlist again and so on.  The answer dropdownlists should be  generated dynamically based on number of questions  in the question table. I can get the questions to print but not the answers list.

My questions are:

1)Is a Dataset solution appropriate for this or should i be using a datareader?
2)Am i on the right track  or is this the appropriate way of what i am trying to achieve?
3)Get the dropdownlists to work.


Code for this follows below:

using.........default namespaces.......
using QuesDSTableAdapters;
using AnsDSTableAdapters;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        QuesDSTableAdapters.QuestionTableAdapter questionsAdapter = new
 QuesDSTableAdapters.QuestionTableAdapter();

        QuesDS.QuestionDataTable questions;

        questions = questionsAdapter.GetQuestions();



        AnsDSTableAdapters.QueAnsTableAdapter queansAdapter = new
 AnsDSTableAdapters.QueAnsTableAdapter();

 AnsDS.QueAnsDataTable answer;

               // AnsDS.QueAnsDataTable queans;


foreach (QuesDS.QuestionRow quesRow in questions)
        {


            Response.Write("Question: " + quesRow.Que + "<br />");

            answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

            DropDownList ddl = new DropDownList() ;

            // ddl.DataSource = queansAdapter.GetAnswersByQuestionId(Convert.ToInt32(quesRow.QID));

           
ddl.DataSource = answer;

           // DropDownList1.Items.Add(Convert.ToString(answer.Columns ["Answer"]));

ddl.DataTextField  = "Answer";

            ddl.DataBind();
           


        }

    }

0
Comment
Question by:ee-itpro
  • 11
  • 9
20 Comments
 
LVL 7

Expert Comment

by:urir10
ID: 24794410
Are you getting anything for the answer DDL?
0
 

Author Comment

by:ee-itpro
ID: 24794441
Didn't understand that.
0
 
LVL 7

Expert Comment

by:urir10
ID: 24794454
Are you getting anything to show in the Drop Down List for the answers or does it just remain blank?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:ee-itpro
ID: 24794499
Remains blank and shows after the two questions that i have currently in  question table.  It should show like ques and then ans nad then ques again and ans again.
0
 
LVL 7

Expert Comment

by:urir10
ID: 24794564
You mean the control itself shows after the two questions are printed? if so you need to add the control to the from within the foreach loop
0
 

Author Comment

by:ee-itpro
ID: 24794709
Yes after the two questions are printed.  
0
 
LVL 7

Expert Comment

by:urir10
ID: 24794755
Where is the line where you adding the control to the page?
0
 

Author Comment

by:ee-itpro
ID: 24794783
i just have it in design view.... how do we add it the code?
0
 

Author Comment

by:ee-itpro
ID: 24794839
i added it just now like this :

DropDownList ddl=new DropDownlist();

form.controls.add(ddl);


Now it shows the lists but there are 3 instead of 2 for 2 questions and they all show in a horizontal line and after the printed questions.
0
 
LVL 7

Expert Comment

by:urir10
ID: 24794938
you can add this between each control and each question to break the line:

form.controls.Add(new LiteralControl("<br/>"));

As to it adding it 3 times it must be something in the loop that makes it run 3 times instead ok.
0
 

Author Comment

by:ee-itpro
ID: 24795122
the dropdownlists still appear after the questions
0
 
LVL 7

Expert Comment

by:urir10
ID: 24795146
can you post your complete code again pls
0
 

Author Comment

by:ee-itpro
ID: 24795182
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        QuesDSTableAdapters.QuestionTableAdapter questionsAdapter = new
 QuesDSTableAdapters.QuestionTableAdapter();
        QuesDS.QuestionDataTable questions;

        questions = questionsAdapter.GetQuestions();



        AnsDSTableAdapters.QueAnsTableAdapter queansAdapter = new
 AnsDSTableAdapters.QueAnsTableAdapter();

        AnsDS.QueAnsDataTable answer;

       

       // AnsDS.QueAnsDataTable queans;

       




        foreach (QuesDS.QuestionRow quesRow in questions)
        {


            Response.Write("Question: " + quesRow.Que + "<br />");



           answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

           Form.Controls.Add(new LiteralControl("<br/>"));

            DropDownList ddl = new DropDownList();


           


            Form.Controls.Add(ddl);

            // ddl.DataSource = queansAdapter.GetAnswersByQuestionId(Convert.ToInt32(quesRow.QID));

            ddl.DataSource = answer;

           // DropDownList1.Items.Add(Convert.ToString(answer.Columns ["Answer"]));
            ddl.DataTextField  = "Answer";
            ddl.DataBind();

           
           
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
0
 
LVL 7

Expert Comment

by:urir10
ID: 24795193
i still dont see where are you adding the control to the page?
0
 

Author Comment

by:ee-itpro
ID: 24795223
after i create the dropdownlist object, then i write

form.control.add(ddl),


where ddl is the dropdownlisty object i created .
0
 
LVL 7

Accepted Solution

by:
urir10 earned 500 total points
ID: 24795245
right sorry. hmm how about if you try inserting the question using

 Form.Controls.Add(new LiteralControl("Question here"));

Instead Response.Write
0
 

Author Comment

by:ee-itpro
ID: 24795408
i added form.control.aa(new literal control........)  in between  ques and ans and also twice before the end of foreach loop. So now it displays better.  But there's still a empty drop downlist that appears  just before the first question.


 foreach (PLabDS.QuestionRow quesRow in questions)
        {


            //Response.Write("Question: " + quesRow.Que + "<br />");



           //answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

           Form.Controls.Add(new LiteralControl(quesRow.Que));

           answer = queansAdapter.GetAnswersByQuestionId(quesRow.QID);

            DropDownList ddl = new DropDownList();


            Form.Controls.Add(new LiteralControl("<br>"));


            Form.Controls.Add(ddl);

            // ddl.DataSource = queansAdapter.GetAnswersByQuestionId(Convert.ToInt32(quesRow.QID));

            ddl.DataSource = answer;

           // DropDownList1.Items.Add(Convert.ToString(answer.Columns ["Answer"]));
            ddl.DataTextField  = "Answer";
            ddl.DataBind();

            Form.Controls.Add(new LiteralControl("<br>"));

            Form.Controls.Add(new LiteralControl("<br>"));
           
        }
0
 

Author Comment

by:ee-itpro
ID: 24795500
deleted the list control from design view . it's working fine now...


i'll accept the solution.



can you just tell me if i am using the right approach (datasets  instead of datareader for this particular scenario)?
0
 

Author Closing Comment

by:ee-itpro
ID: 31600551
deleted the list control from design view . it's working fine now...


i'll accept the solution.



can you just tell me if i am using the right approach (datasets  instead of datareader for this particular scenario)?
0
 
LVL 7

Expert Comment

by:urir10
ID: 24796203
Yes a dataset or a datatables are just fine.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

832 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