ee-itpro
asked on
Generate Dropdownlists dynamically and bind data from datatable or dataadapter methods
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)con taing 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.Questi onTableAda pter questionsAdapter = new
QuesDSTableAdapters.Questi onTableAda pter();
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti ons();
AnsDSTableAdapters.QueAnsT ableAdapte r queansAdapter = new
AnsDSTableAdapters.QueAnsT ableAdapte r();
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
Response.Write("Question: " + quesRow.Que + "<br />");
answer = queansAdapter.GetAnswersBy QuestionId (quesRow.Q ID);
DropDownList ddl = new DropDownList() ;
// ddl.DataSource = queansAdapter.GetAnswersBy QuestionId (Convert.T oInt32(que sRow.QID)) ;
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co nvert.ToSt ring(answe r.Columns ["Answer"]));
ddl.DataTextField = "Answer";
ddl.DataBind();
}
}
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)con
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.Questi
QuesDSTableAdapters.Questi
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti
AnsDSTableAdapters.QueAnsT
AnsDSTableAdapters.QueAnsT
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
Response.Write("Question: " + quesRow.Que + "<br />");
answer = queansAdapter.GetAnswersBy
DropDownList ddl = new DropDownList() ;
// ddl.DataSource = queansAdapter.GetAnswersBy
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co
ddl.DataTextField = "Answer";
ddl.DataBind();
}
}
Are you getting anything for the answer DDL?
ASKER
Didn't understand that.
Are you getting anything to show in the Drop Down List for the answers or does it just remain blank?
ASKER
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.
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
ASKER
Yes after the two questions are printed.
Where is the line where you adding the control to the page?
ASKER
i just have it in design view.... how do we add it the code?
ASKER
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.
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.
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.
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.
ASKER
the dropdownlists still appear after the questions
can you post your complete code again pls
ASKER
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
QuesDSTableAdapters.Questi onTableAda pter questionsAdapter = new
QuesDSTableAdapters.Questi onTableAda pter();
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti ons();
AnsDSTableAdapters.QueAnsT ableAdapte r queansAdapter = new
AnsDSTableAdapters.QueAnsT ableAdapte r();
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
Response.Write("Question: " + quesRow.Que + "<br />");
answer = queansAdapter.GetAnswersBy QuestionId (quesRow.Q ID);
Form.Controls.Add(new LiteralControl("<br/>"));
DropDownList ddl = new DropDownList();
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy QuestionId (Convert.T oInt32(que sRow.QID)) ;
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co nvert.ToSt ring(answe r.Columns ["Answer"]));
ddl.DataTextField = "Answer";
ddl.DataBind();
}
}
protected void DropDownList1_SelectedInde xChanged(o bject sender, EventArgs e)
{
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
QuesDSTableAdapters.Questi
QuesDSTableAdapters.Questi
QuesDS.QuestionDataTable questions;
questions = questionsAdapter.GetQuesti
AnsDSTableAdapters.QueAnsT
AnsDSTableAdapters.QueAnsT
AnsDS.QueAnsDataTable answer;
// AnsDS.QueAnsDataTable queans;
foreach (QuesDS.QuestionRow quesRow in questions)
{
Response.Write("Question: " + quesRow.Que + "<br />");
answer = queansAdapter.GetAnswersBy
Form.Controls.Add(new LiteralControl("<br/>"));
DropDownList ddl = new DropDownList();
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co
ddl.DataTextField = "Answer";
ddl.DataBind();
}
}
protected void DropDownList1_SelectedInde
{
}
}
i still dont see where are you adding the control to the page?
ASKER
after i create the dropdownlist object, then i write
form.control.add(ddl),
where ddl is the dropdownlisty object i created .
form.control.add(ddl),
where ddl is the dropdownlisty object i created .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.GetAnswersBy QuestionId (quesRow.Q ID);
Form.Controls.Add(new LiteralControl(quesRow.Que ));
answer = queansAdapter.GetAnswersBy QuestionId (quesRow.Q ID);
DropDownList ddl = new DropDownList();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy QuestionId (Convert.T oInt32(que sRow.QID)) ;
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co nvert.ToSt ring(answe r.Columns ["Answer"]));
ddl.DataTextField = "Answer";
ddl.DataBind();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(new LiteralControl("<br>"));
}
foreach (PLabDS.QuestionRow quesRow in questions)
{
//Response.Write("Question
//answer = queansAdapter.GetAnswersBy
Form.Controls.Add(new LiteralControl(quesRow.Que
answer = queansAdapter.GetAnswersBy
DropDownList ddl = new DropDownList();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(ddl);
// ddl.DataSource = queansAdapter.GetAnswersBy
ddl.DataSource = answer;
// DropDownList1.Items.Add(Co
ddl.DataTextField = "Answer";
ddl.DataBind();
Form.Controls.Add(new LiteralControl("<br>"));
Form.Controls.Add(new LiteralControl("<br>"));
}
ASKER
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)?
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)?
ASKER
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)?
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)?
Yes a dataset or a datatables are just fine.