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

Login And Membership

Since on the home page must be the login form (I decided to rename it to Login.aspx)


Visual studio has a Login  control designed especially for this purpose.
Everybody knows that login form have to work aside with data tables.
Fortunately there is a way to create quickly all those tables:


There is built in tool for creating: users, roles and passowrds in Visual Studio named Web Tool.
You can start it from Solution Explorer window: After Selecting the Project node click the:




Or you can place the login control (as i did) and you can start this tool from the control itself .





This tool gives you the simple interface there you can add users, define roles and permissions.
This is how i defined two roles:

Sims very simple business...

But it not so simple.
All the roles users and their passwords are automatically stored in database named ASPNETDB.mdf. This database appears after you run the Webtool and created your configuration
And it not so simple to configure the Visual Studio in the way it will create all the membership stuff in my database.

Here
you can read about how to make membership tables in the same database


So now i have two Databases: M-survey.mdf and ASPNETDB.mdf. Until now it not a big problem, so let it stay  this way…
I made two directories to plase there pages that be visible only to registered users:





The premissions that i gave make the pages in Admin directory be visible only to adminstrator, and pages in Users directory are visible only to 'members'.

web.config page:


<authentication mode="Forms">
      <forms loginUrl="login.aspx"
defaultUrl="Users/Default.aspx"/>
</authentication>





This code makes the authenticated user to go firstly to users default page


    protected void Page_Load(object sender, EventArgs e)
     {
        if (User.Identity.IsAuthenticated)
        {
            if (User.IsInRole("Administrator"))
            {
                Response.Redirect("../Admin/Admin.aspx");

             }
        }
    }



This code i place at  users\default page (Survey), To make user (if the user is Administrator) - to be redirected to Admin\Admin.aspx

web.config:

  <location path="Admin">
   <system.web>
    <authorization>
     <allow roles="Administrators"/>
     <deny users="*"/>
   </authorization>
  </system.web>
  </location>
  <location path="Users">
    <system.web>
    <authorization>
       <allow roles="members"/>
      <deny users="*"/>
    </authorization>
  </system.web>
 </location>


This code prevents unauthenticated users to see the user's or administrator's pages

Finally:
So now we have a minimalistic login interface:
from page "login.aspx" users can sign up and be redirected to empty (meanwhile) "Default.aspx" page, whereas administrator, after signing up will see the Admin.aspx page.

Here you can download the source link

The Home Page

At least I come to visual studio  step...
File -> New -> Website.

I have created and attached  database.
(App_Data ->Add New Item -> Sql Server Database)
and give it name M-survey.


Here is Database tables screenshot:


You can see here two mor tables i didn't mention:
Types - it is for question types dropdown control.
TempAnswers - i will explain about it later .


 So its seems a good start so far...

Making the pages


I dont like and dont have the time and skills for designing. So i will take the interface design from the Great Joomla.{I'm a fan of Joomla}
Here is gallery of pages:
Home

Survey




build_Question


Admin_home


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...