Skip to content

Displaying and inserting Quiz Items

Matias aabye

Asked May 9, 2013, 4:39 PM EDT6 answers
Share
LinkedInX
Hello Everyone. 

I am really new to Visusal Studio / C# / ASP. 

But currently i am doing a little test project, to make a website, with a quiz, where users can go in and fill out information.

I've made the backend, to insert new questions/Answers etc. 

My databases is as following: 

Questions:  ( the tabel for the questions name) 
- Questionsid
- QuestionsName

QuizAnswers ( the tabel for all the differently answers, to each question, that a Administrator create) )
- QuizAnswerID
- QuestionsID
- QuizAnswer
- Correct (True/False) 

But my big problem is this. 

I am used to Dreamweaver and php.

I need to show all the Questions from Questions tabel, and include the answers below the questions. 
The key to combine the 2 tabels are the: "QuestionsID". 
I've searched all of google, with no real results, and right now i am lost... 

So my problem is: 
1. How do i display the questions and the answers, and at the same time to combine the 2 tabels, with the QuestionsId. 
2. How do i repeat that, so that i can repeat the region, until the questions tabel is empty, like if i want to show 10 questions, and all the answers to each question. 

Ex: 
What color is bla bla: (Question name is from the Question tabel) 
1 - Red (The answer is from the Answers tabel) 
2- Blue (The answer is from the Answers tabel) 


Regards
Matias

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You have to joined this two table(using INNER JOIN). Take the following pseudocode to gain an idea.

    Response.Write("");

    For Each DataRow in DataTable
           
           
            
           
            
           

    End For
    Response.Write("
    Display in a table row DataRow Index 0        //Question
           
    Display in a table row DataRow Index 1        //Answer A
            
    Display in a table row DataRow Index 2        //Answer B
           
    Display in a table row DataRow Index 3        //Answer A
            
    Display in a table row DataRow Index 4        //Answer A
           
    ");
    0
  • 0
    But will that not give some problems. If we say, that there are only 10 rows in Questions tabel (Because 10 questions name), but meanwhile there are 40 answers (4 Answer/choice to each question). Would that not give some problems?
    0
  • 0
    That won't give a problem, since all you need to do is to display your questions including their answers to choose for.
    0
  • 0
    Alright, i get the idea. But can i get you to give a little more hint on the code? As i get the full idea, but got some problems to include it into the file.
    0
  • 0
    public void LoadQuiz()
            {
                DataTable dt = new DataTable();
                //Declare your connection obj
                SqlConnection connObject = new SqlConnection();
                //Declare you command object
                SqlCommand cmdObject = new SqlCommand();
               
                //Assign connection to your connection
                cmdObject.Connection = connObject;
                //Use ExecuteReader to retrieve records
                cmdObject.CommandText = "SELECT Table1.QuestionName, Table2.QuizAnswer1, Table2.QuizAnswer2, Table2.QuizAnswer3, Table2.QuizAnswer4 FROM Table1 INNER JOIN Table2 ON Table1.QuestionId=Table2.QuestionId";
                SqlDataAdapter ad = new SqlDataAdapter();
                ad.Fill(dt);

                Response.Write("");
               
                foreach (DataRow dr in dt.Rows)
                {
                    Response.Write("");
                    Response.Write("");
                    Response.Write("");
                    Response.Write("");
                    Response.Write("");
                    Response.Write("");
                }

                Response.Write("
    " + dr[0].ToString());      //Question
                    Response.Write("
    " + dr[1].ToString());                  //Choice 1
                    Response.Write("
    " + dr[2].ToString());                  //Choice 2
                    Response.Write("
    " + dr[3].ToString());                  //Choice 3
                    Response.Write("
    " + dr[4].ToString());                  //Choice 4
                    Response.Write("
    ");

                dt.Dispose();
                ad.Dispose();
                cmdObject.Dispose();
                connObject.Close();
            }
    0
  • 0
    I've ended up with this: 

    In CS PAGE: 
    Java
      //Create the connection and DataAdapter for the Authors table.
                SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["QuizDataConnectionString1"].ConnectionString);
                SqlDataAdapter cmd1 = new SqlDataAdapter("select * from Questions", cnn);
    
    
                //Create and fill the DataSet.
                DataSet ds = new DataSet();
                cmd1.Fill(ds, "Questions");
    
    
                //Create a second DataAdapter for the Titles table.
                SqlDataAdapter cmd2 = new SqlDataAdapter("select * from QuizAnswers", cnn);
                cmd2.Fill(ds, "QuizAnswers");
    
    
                //Create the relation bewtween the Authors and Titles tables.
                ds.Relations.Add("myrelation",
                ds.Tables["Questions"].Columns["Questionsid"],
    C#
                ds.Tables["QuizAnswers"].Columns["QuestionID"]);
    
    
                //Bind the Authors table to the parent Repeater control, and call DataBind.
                parentRepeater.DataSource = ds.Tables["Questions"];
                Page.DataBind();
    
    
                //Close the connection.
                cnn.Close()

    On main page: 
       
         
              <%# DataBinder.Eval(Container.DataItem,"QuestionsName") %>
         

         
         

             

                 
                    SelectedValue='<%# DataBinder.Eval(Container.DataItem, "[\"QuizAnswerid\"]")%>' runat="server" TextAlign="Right" />
    0