Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
I am conducting exam . So i retrieve questions from the database. i get the total count of the it is 10 , i have created ten radiobuttonlist in the web form so i retrieved data from database and added it to the ten radiobuttonlist that was added to the form . but if the question count in the database is increasing dynamically accordingly how to automatically /dynamically create radiobuttonlist and bind it to the database values Frown | :( can u please help me out Frown | :( this is my project , Frown | :(
 

my query looks like
 
 SqlCommand cmd = new SqlCommand("select ROWNUMBER,questionno,tname,question,ans1,ans2,ans3,ans4 from questions where tname='Prelim' and questionno='" + q + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "questions");
 
                DataRow DR = ds.Tables[0].Rows[0];
 
                DataTable d = new DataTable();
 
                labell.Text = Session["New"].ToString();
                Question.Text = totalQs.ToString();
                sno = DR[1].ToString();
 
                TestName.Text = DR[2].ToString();
 
                for (int i = 1; i <= 10; i++)
                {
 
                    SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname='Prelim' and questionno='" + i + "'", con);
                    SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                    DataSet dss = new DataSet();
                    daa.Fill(dss, "questions");
 
                    DataRow DRR = dss.Tables[0].Rows[0];
 
                    if (i == 1)
                    {
                        RadioButtonList1.Items.Clear();
                        RadioButtonList1.Items.Add(DRR[0].ToString());
                        RadioButtonList1.Items.Add(DRR[1].ToString());
                        RadioButtonList1.Items.Add(DRR[2].ToString());
                        RadioButtonList1.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 2)
                    {
                        RadioButtonList2.Items.Clear();
                        RadioButtonList2.Items.Add(DRR[0].ToString());
                        RadioButtonList2.Items.Add(DRR[1].ToString());
                        RadioButtonList2.Items.Add(DRR[2].ToString());
                        RadioButtonList2.Items.Add(DRR[3].ToString());
                    }
                    else if (i == 3)
                    {
so on
 

But those radiobuttonlist1,radiobuttonlist2 are all predefined/created in the form that is the limitation :(
Posted 20 Feb '13 - 7:38
Edited 20 Feb '13 - 8:50
richcb12.6K

Comments
richcb - 20 Feb '13 - 13:42
After querying the questions, get the total number and create a radiobutton list for each one created. So use a foreach loop once you have the count of the questions and make a radiobutton list for each question in your questions collection.
Member 8780842 - 20 Feb '13 - 13:43
yes but how to make a radiobuttonlist dynamically where it will be created how to create it dynamically :( can u provide me a code snippet pls
Member 8780842 - 20 Feb '13 - 13:53
richcb: rb1 is the radiobuttonlist . whether it will be posted in the web form directly
Member 8780842 - 20 Feb '13 - 14:04
Ya you are right :( but i read that i cant figure it out how that can be applied in my scenario :( . i have got the total count of questions for one round by query and stored in totalQs . In the database i have the fields as tname,Question, ans1,ans2,ans3,ans4,cans tname round1 question is the question ans1,2,3,4 are the choices cans is the correct ans Now i want to show them in this order //question1 //choices of question1 //question2 //choices of question2 .. so on how can i display them in this order :( i have tried many times and spent most of my sleep in this project :( please help me
richcb - 20 Feb '13 - 14:42
What does your query look like? Use the "Improve question" widget to add the code so we can see what you are doing.
Member 8780842 - 20 Feb '13 - 14:48
i have updated my question

3 solutions

Here is the updated version that is not fully what you want, but the for loop has the logic to create radiobuttonlists dynamically where "Form1" is the name of your form. If you need more help, ask.
 
 
                    SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname='Prelim' and questionno='" + i + "'", con);
                    SqlDataAdapter daa = new SqlDataAdapter(cmd1);
                    DataSet dss = new DataSet();
                    daa.Fill(dss, "questions");
 
                    DataRow DRR = dss.Tables[0].Rows[0];
 
            for (int i = 0; i <= dss.Tables[0].Rows.Count; i++)
                {
                    RadioButtionList rbl = new RadioButtonList();
                    rbl.ID = "rbl" + i;
 
 
                    if (i == 1)
                    {
                        rbl1.Items.Clear();
                        rbl1.Items.Add(DRR[0].ToString());
                        rbl1.Items.Add(DRR[1].ToString());
                        rbl1.Items.Add(DRR[2].ToString());
                        rbl1.Items.Add(DRR[3].ToString());
                        Form1.Controls.Add(rbl1);
                    }
                    else if (i == 2)
                    {
                        rbl2.Items.Clear();
                        rbl2.Items.Add(DRR[0].ToString());
                        rbl2.Items.Add(DRR[1].ToString());
                        rbl2.Items.Add(DRR[2].ToString());
                        rbl2.Items.Add(DRR[3].ToString());
                        Form1.Controls.Add(rbl2);
                    }
 
  Permalink  
Comments
Member 8780842 - 22 Feb '13 - 7:25
Thank you very much but small doubt you have used Form1.Controls.Add .. I am using asp.net will it show an error
richcb - 22 Feb '13 - 9:39
I have an asp.net application that creates link buttons dynamically based on a collection amount just like what you are doing. The line of code you are concerned with is what actually adds the dynamically created radiobuttonlist to the page.
Member 8780842 - 23 Feb '13 - 1:06
Thank you Working fine :) :) :) I modified it to SqlCommand cmd1 = new SqlCommand("select ans1,ans2,ans3,ans4,cans from questions where tname='Prelim' and questionno='" + i + "'", con); SqlDataAdapter daa = new SqlDataAdapter(cmd1); DataSet dss = new DataSet(); daa.Fill(dss, "questions"); DataRow DRR = dss.Tables[0].Rows[0]; Response.Write(i + DRR[4].ToString()); RadioButtonList rbl = new RadioButtonList(); rbl.ID = "rb1" + i; rbl.Items.Clear(); rbl.Items.Add(DRR[0].ToString()); rbl.Items.Add(DRR[1].ToString()); rbl.Items.Add(DRR[2].ToString()); rbl.Items.Add(DRR[3].ToString()); form1.Controls.Add(rbl); } Working fine and great thank youuuuuuuuuuuu very much richcb and another doubt in between i have to display questions in numbered order So i added the line "Response.Write(i + DRR[4].ToString());" it was displaying all the questions in two lines and den all the radiobuttonlist :(
Member 8780842 - 23 Feb '13 - 1:29
I got it :) :) :) I used Label.text and added it dynamically :) Thank you richcb thank you very much for teaching me this dynamic concept that i forgot to implement at this time :) :) :)
Member 8780842 - 23 Feb '13 - 4:12
richch one small help now i have created the radiobuttonlist boxes dynamically ....but now i have to cal the total marks from the selected options ,in my examination system . i used the following code in one function cal() and call it the cal method is as follow but i am getting the score as zero only i dont know where is the problem in my code :( protected void cal() { int cs = 0; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); con.Open(); TimeSpan time = (Session["CountdownTimer"] as CountDownTimer).TimeLeft; TimeSpan time2 = TimeSpan.Parse("00:10:00"); string timel = time2.Subtract(time).ToString(); string ref1 = Session["New"].ToString(); SqlCommand cmd = new SqlCommand("select * from questions where tname='Prelim'", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "questions"); DataRow DR = ds.Tables[0].Rows[0]; for (int i = 1; i <= ds.Tables[0].Rows.Count; i++) { SqlCommand cmd1 = new SqlCommand("select cans from questions where tname='Prelim' and questionno='" + i + "'", con); SqlDataAdapter daa = new SqlDataAdapter(cmd1); DataSet dss = new DataSet(); daa.Fill(dss, "questions"); DataRow DRR = dss.Tables[0].Rows[0]; rbl.ID = "rbl" + i; if (rbl.SelectedIndex > -1) { string gota = DRR[0].ToString(); string selval = rbl.SelectedItem.Text; if (gota.Equals(selval)) { cs = cs + mk; } else { cs = cs - 1; } } else { cs = cs + 0; } } string insCmd1 = "Insert into Result(UserName,Test1Mark,Test1Time) values(@UserName,@Test1Mark,@Test1Time) "; SqlCommand insertUser = new SqlCommand(insCmd1, con); insertUser.Parameters.AddWithValue("@UserName", ref1.ToString()); insertUser.Parameters.AddWithValue("@Test1Mark", cs.ToString()); insertUser.Parameters.AddWithValue("@Test1Time", timel.ToString()); insertUser.ExecuteNonQuery(); con.Close(); }
richcb - 23 Feb '13 - 12:17
Accept my solution as your answer if it worked for you please.
Member 8780842 - 2 Mar '13 - 2:40
:) accepted :)
I would suggest that you use a repeater on your asp.net form and then build the repeater component to handle all the different setups for different questions. ie. a question with 1 or more answers, but not necessarily the same number of answers for each.
Data Repeater Control[^]
  Permalink  
Comments
Member 8780842 - 20 Feb '13 - 14:06
Ya you are right :( but i read that i cant figure it out how that can be applied in my scenario :( . i have got the total count of questions for one round by query and stored in totalQs . In the database i have the fields as tname,Question, ans1,ans2,ans3,ans4,cans tname round1 question is the question ans1,2,3,4 are the choices cans is the correct ans Now i want to show them in this order //question1 //choices of question1 //question2 //choices of question2 .. so on how can i display them in this order :( i have tried many times and spent most of my sleep in this project :( please help me
Marcus Kramer - 20 Feb '13 - 14:08
So, what's the problem. Just retrieve a dataset containing the questions you want for a given page and then bind them to the repeater which will display just those questions. To display them in a given order you need to implement the "ORDER BY" clause in your sql when you retrieve them.
Why you are not using repeater control? use repeater control put radio button in it and retrieve data from database in dataset and just assign that dataset as a datasource to repeater, so it will automatically generate number radio button equal to record in your dataset...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Sergey Alexandrovich Kryukov 169
2 Tadit Dash 154
3 Richard MacCutchan 145
4 Santhosh G_ 125
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid