9/22/09

Displaying all the questions of the survey

In this page we want user to see all the questions of certain survey (and answer them).
I chose to do it in following way:
First :
This Store Procedure gets all the answers with their question aside.

ALTER PROCEDURE dbo.AllQuestionsOfCurrentSurvey
@SurveyID int



AS
            SELECT
            [questions].[QuestionID],
            [questions].[type],
            [answers].[answer],
            [answers].[answerID],
            [questions].[question]             
      FROM [answers] LEFT JOIN [questions]
            ON [answers].[quest_id] = [questions].[questionID]
      WHERE
            ([questions].[surv_id] =@SurveyID)
      RETURN
Now we need to place this data on the page:
The question with answer options aside.



   private void LoadData()
    {
                   
                  StringBuilder sb = new StringBuilder();
                  //string myQuery ="";
                 


                 
                 
                  clsDataAccess myclass = new clsDataAccess();
                  myclass.openConnection();
                 
                 
                 
                  SqlDataReader myReader = myclass.getSurveyData(1);

                  //int mycount=1;
            int questionID = 0;
                  int new_questionID = 0;
            int question_type=0;
            bool firstTime=true;

                  while (myReader.Read() )
                  {


                new_questionID =Convert.ToInt32(myReader["questionID"]);
                question_type=Convert.ToInt32(myReader["type"]);



                if (questionID != new_questionID)//starting new question
                {
                    questionID = new_questionID;


                   
                    if (!firstTime)//to close the prevous table
                    {
                        sb.Append("</table>");
                    }
                    else
                    {
                        firstTime = false;
                    }

                


                    //display quetion:
                    sb.Append("<table width='100%' bgcolor='blue'>");
                    sb.Append("<tr>");
                    sb.Append("<td colspan=2 bgcolor='white'>");
                    sb.Append(myReader["question"].ToString());
                    sb.Append("</td>");
                    sb.Append("</tr>");
                   
                }


                    //dispaly all the answers:


                   //dispaly different inputs for each question type
                   
                    switch(question_type)
                    {
                        case 1: //chose many options answer
                            sb.Append("<tr>");
                            sb.Append("<td bgcolor='white' width='90%' align='left'>");
                            sb.Append(myReader["answer"].ToString());
                            sb.Append("</td>");
                            sb.Append("<td bgcolor='white' align='center'>");
                            sb.Append("<INPUT TYPE=RADIO NAME='q_" + questionID + "'  value='"+Convert.ToInt32(myReader["type"])+"'/>");
                            sb.Append("</td>");
                            sb.Append("</tr>");
                            break;

                        case 2://select one option answer
                            sb.Append("<tr>");
                            sb.Append("<td bgcolor='white' width='90%' align='left'>");
                            sb.Append(myReader["answer"].ToString());
                            sb.Append("</td>");
                            sb.Append("<td bgcolor='white' align='center'>");
                            sb.Append("<input type='checkbox' name='q_" + questionID + "'   />");
                            sb.Append("</td>");
                            sb.Append("</tr>");
                            break;

                        case 3: //text answer
                            sb.Append("<tr>");
                            sb.Append("<td bgcolor='white' width='90%' align='left' colspan=2>");
                            sb.Append("<textarea cols='50' rows='5' name='q_" + questionID + "' ></textarea>");
                            sb.Append("</td>");
                            sb.Append("</tr>");
                            break;
                    }


               
                   

           
            }//end of while

            //care about the last record
            sb.Append(
"</table>");
            Questions.Add(oQuestion);

 

            myReader.Close();
            myclass.closeConnection();

            LtlQuestionForm.Text = sb.ToString();
    }//end of LoadData
}
Note that all the html is sended as text property to Literal control (currently single control on the page)

Finally
Now users (in members role) can view now all the questions of survey (currently survey one) displayed






To dowload the source: link

No comments:

Post a Comment

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...