Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi I am a fresher in asp.net.
In the below situation i want to display question when the button is clicked. I used for loop. But the problem is i can able to display only the last question and option. But if i use response.write all the 3 question and option are displayed. I have placed my code and output below.
Can any one please help me on this.

In program.cs
C#
protected void Button1_Click1(object sender, EventArgs e)
   {
       DatabaseDLL.Quiz ObjQuiz = DatabaseDLL.Manager.DisplayQuestions("General Knowledge");
       if (IsPostBack)
       {
           foreach (DatabaseDLL.Question Q in ObjQuiz.GetQuestions())
           {
               Response.Write(Q.Question1);
               Response.Write(Q.Option1);
               Response.Write(Q.Option2);

               Label1.Text = Q.Question1;
               Label2.Text = Q.Option1;
               Label3.Text = Q.Option2;

           }
       }

   }

In the dll manager class:
C#
public static Quiz DisplayQuestions(string category)
      {
          Quiz ObjQuiz = new Quiz();
          try
          {
              Conn = new SqlConnection("Data Source=HDCHARTOMR1213\\SQLEXPRESS;Initial Catalog=UserDetails;Integrated Security=SSPI");
              Conn.Open();
              SqlCommand Cmd = new SqlCommand("QuizQuestions", Conn);
              Cmd.CommandType = CommandType.StoredProcedure;
              Cmd.Parameters.AddWithValue("@Category", category);
              SqlDataReader reader = Cmd.ExecuteReader();
              while (reader.Read())
              {

                  Question ObjQuestion = new Question(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString());
                  ObjQuiz.AddQuestion(ObjQuestion);
              }

              reader.Close();

          }
          catch (Exception e)
          {
              Console.WriteLine(e);
          }
          finally
          {
              if (Conn != null)
              {
                  Conn.Close();
              }
          }
          return ObjQuiz;

      }

Question Class:
C#
public class Question
   {
        public string Option1 { get; private set; }
        public string Option2 { get; private set; }
        public string Question1 { get; private set; }
        public string Category { get; private set; }
        public Question(string Question1, string Option1, string Option2, string Category)
        {
            this.Question1 = Question1;
            this.Option1 = Option1;
            this.Option2 = Option2;
            this.Category = Category;
        }

   }

Quiz Class:
C#
public class Quiz
    {
       public List<question> ObjQuestion = new List<question>();
       public void AddQuestion(Question NewQuestion)
       {
           ObjQuestion.Add(NewQuestion);
       }
       public List<question> GetQuestions()
       {
           return ObjQuestion;
       } 
        public Quiz()
        {
           
        }
    }

Output:

After Button click i get the following output below:

//for response.write
Who is the Father of our Nation
Mahathma Gandhi
Jawaharlal Nehru
What is our National Animal
Lion
Tiger
What is our National Flower
Lotus
Rose



//for the label only last Question and options are displayed
What is our National Flower //label1

Lotus //label2

Rose //label3

Thank You!!
Posted
Updated 2-Apr-12 19:42pm
v2
Comments
Anderson8055 9-Apr-12 8:48am    
Thanks for Giving idea for me toget solution!!

It doesn't work like that.

When you use a for or foreach loop it executes completely - so you will display each of the questions for a minute period of time before showing only the last one - it having overwritten all the others.

What you need to do is have a Session variable or cookie (depending on how your application is meant to work) which stores the question you are on (and probably a separate one which records the answers so far). You then read that and display only that question.

Since this is your homework, I won't give you any code: but it's not complex, and should be covered by your lecture notes anyway.
 
Share this answer
 
Comments
vishul garg 3-Apr-12 4:22am    
wfuehfhefhehg
OriginalGriff 3-Apr-12 4:28am    
Thank you for that - does your mother know you are out on your own?
Anderson8055 3-Apr-12 6:17am    
Thanks Griff.But i will be happy if i get code for this!!
OriginalGriff 3-Apr-12 6:26am    
Nope! We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action. :laugh:

Try it - it isn't difficult, and you will learn better how to do it next time!
Anderson8055 3-Apr-12 6:36am    
Ok Griff i will take your words and think... Thank you!!!
I tried. But i am not getting it..
If any one can find solution help me..
Thanks!
 
Share this answer
 
Get the values from database using executereader() and store it in DataTable.
Now from the DataTable we can get the columns we want and display in textbox and radiobutton.
The solution has become simple..
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900