Develop a Survey Application: Part I

Getting Started

Bipin Joshi

October 30, 2009

11 Min Read
ITPro Today logo in a gray background | ITPro Today

CodeTalk

LANGUAGES:VB.NET

ASP.NETVERSIONS: 2.0

 

Develop a Survey Application: Part I

Getting Started

 

By Bipin Joshi

 

In today s competitive environment companies find itnecessary to get feedback on their products and services. Companies also needto know the market trends, as well as customer likes and dislikes. Companies oftencapture this vital information by conducting surveys about their products andservices. A flexible Web-based survey system plays a major role in suchscenarios. In this series we are going to develop a survey application usingASP.NET 2.0, VS.NET 2005, and SQL Server 2005. We ll kick things off in Part I witha look at the functional requirements and database schema, then create thesurvey definitions.

 

Functional Requirements

Because a survey application captures various pieces ofinformation from the end user it must be flexible and easy to use. A surveyconsists of questions and feedback or answers to those questions. The answerscan be the objective type or in the form of comments. The application we aregoing to develop will satisfy the following functional requirements:

  • The administrator should be able to create asurvey definition in the system.

  • Each survey consists of a set of questions.There should be Web pages that allow for the addition/modification/deletion ofquestions and their possible choices.

  • The answer to a question can be single choice,multiple choice, or free text.

  • Depending on the answer type, the end usershould be presented with radio buttons, checkboxes, or multi-line textboxes atthe time of taking the survey.

  • Each survey has certain participants. Thereshould be a Web page for managing participants and their e-mail addresses.

  • Once a survey is ready with its questions andparticipants the administrator should be able to send an invitation to all theparticipants via e-mail. The invitation will consist of a URL wherein the usercan take the survey.

  • Once the end user goes to the survey URL heshould see all the questions from the survey, along with their choices.

  • Once the user submits the survey the answersshould be saved for generating reports.

  • The administrator should be able to see thesurvey statistics for every objective type (single choice or multiple choice)question.

  • The administrative pages should be protectedfrom unauthorized users.

 

We ll develop this application using ASP.NET 2.0, ADO.NET2.0, and SQL Server 2005 Express Edition. Specifically, we ll use the followingfeatures:

  • SQL Data Source control

  • Databound controls: GridView, DetailsView, and DataList

  • Membership features for securing administrativepages

  • TreeView control for navigation

  • Sending e-mails

  • ADO.NET (SQL Server Data Provider) objects toexecute queries wherever required

 

To begin, create a new ASP.NET Web site using VisualStudio .NET or Visual Web Developer. Make sure to choose a language, such asVisual Basic, and name the site Survey . Once the Web site is created, createa folder named Admin inside the Web site root folder. All the administrativepages will be stored in this folder. The other pages accessible to all theusers will be stored directly under the root folder.

 

Database Schema

All the application data will be stored in a SQL Server2005 database. To add a new database to the Web site right click on the Website, select Add New Items, then select SQL Database from the dialog box. Name thedatabase Database1.mdf. Once you have a database in your Web site you cancreate tables. Figure 1 shows the schema of various tables and Figure 2 liststhe table names and descriptions.

 


Figure 1: Database schema.

 

Table Name

Description

Survey

This table stores the survey definition in the form of SurveyID, Name, and Description.

SurveyQuestions

This table contains a list of survey questions. It also specifies the answer type for each question: Single choice (S), Multiple choice (M), or Text (T).

SurveyChoices

This table contains possible choices for single-choice and multiple-choice questions.

SurveyAnswers

Once the user takes a survey, the answers to single-choice and multiple-choice questions, as well as free text answers, are stored in this table.

SurveyParticipants

This table contains a list of participants for a survey, along with their name and e-mail address.

Figure 2: Tablenames and descriptions.

 

We need to develop seven Web forms and one master page forthis project; Figure 3 lists each one, along with a corresponding folder and description.

 

Web Form/Master Page

Folder

Description

ManageSurvey.aspx

Admin

This Web form allows the administrator to create, edit, and delete survey definitions. It also has a facility to send invitations to all the survey participants.

ManageQuestions.aspx

Admin

This Web form is used to add, edit, and delete questions belonging to a survey. Here, you also specify the answer type for each question.

ManageChoices.aspx

Admin

This Web form allows the administrator to add, edit, and delete choices for each question. Note that choices are applicable only for single- and multiple-choice questions, not for questions with free-text answers.

ManageParticipants.aspx

Admin

This Web form allows the administrator to add, edit, or delete participants of a survey. The name and e-mail of each participant is entered via this Web form. The information gathered via this Web form is used while sending survey invitations.

SurveyStats.aspx

Admin

The administrator would be interested to know the collective result of the surveys. This Web form displays such statistics. Only single-choice and multiple-choice questions are considered for this purpose.

Login.aspx

Root

The administrative pages are accessible only to the administrator. This page allows the administrator to log in to the system.

Survey.aspx

Root

This Web form takes a survey ID as a query string parameter and displays questions for that survey to the end user. Depending on the answer type of a question, it renders RadioButtons, CheckBoxes, or TextBoxes. The URL to this Web form is sent to the participants as a part of the e-mail invitation.

AdminMasterPage.master

Admin

This master page represents the master page of all the administrative Web forms. It presents a TreeView with links to all the administrative Web forms.

Figure 3: List of Webforms and master pages.

 

Creating the Master Page

All the administrative pages use a master page namedMasterPage.master. This master page provides the navigation tree and overalllayout to the other pages. To create this master page, add a new master page inthe Admin folder of the Web site using the Add New Item dialog box (see Figure 4).Figure 5 shows the master page in design mode.

 


Figure 4: Adding a master page.

 


Figure 5: The master page in designmode.

 

The master page consists of a TreeView control with nodespointing to other administrative pages. To design the master page simply dragand drop a TreeView control on the master page. Choose Edit Nodes from itssmart tag to open the TreeView Node Editor (see Figure 6). Then add nodes tothe TreeView so as to provide navigation structure for all the administrativepages. Set the Text and NavigateUrl properties of various nodes as shown inFigure 7. Figure 8 shows the complete markup of the master page.

 


Figure 6: The TreeView Node Editor.

 

TreeView Node

Text

NavigateUrl

Manage Survey

Manage Survey

~/Admin/managesurvey.aspx

Manage Questions

Manage Questions

~/Admin/managequestions.aspx

Manage Choices

Manage Choices

~/Admin/managechoices.aspx

Manage Participants

Manage Participants

~/Admin/manageparticipants.aspx

Survey statistics

Statistics

~/Admin/surveystats.aspx

Figure 7:Properties of various TreeView nodes.

 

<%@ Master Language="VB"CodeFile="AdminMasterPage.master.vb"

 Inherits="Admin_AdminMasterPage" %>

   

 ImageUrl="~/Images/logo.gif">

 ImageSet="WindowsHelp">  HorizontalPadding="0px" VerticalPadding="0px" />  NavigateUrl="~/Admin/ManageSurvey.aspx">  NavigateUrl="~/Admin/ManageQuestions.aspx">  Value="ManageParticipants" NavigateUrl="  ~/Admin/ManageParticipants.aspx">  Text="Statistics"Value="Statistics">  ForeColor="Black"HorizontalPadding="5px"  NodeSpacing="0px"VerticalPadding="1px" />

 runat="server">

 Text="Copyright (C)2006. All rights reserved.">

Figure 8: Markupof master page.

 

Managing Surveys

The first step in a survey campaign is to create a survey.We ll create a Web form that allows us to add, edit, and delete surveys.Proceed by adding a new Web form named Survey.aspx in the Admin folder. Theoverall layout of the Web form is shown in Figure 9.

 


Figure 9: Managing the surveydefinition.

 

To design the Web form, drag and drop a SQL data source control(SqlDataSource1) on the Web form and configure it to select all the recordsfrom the Survey table (see Figure 10).

 


Figure 10: Configuring a SQL datasource.

 

Then click the Advanced button and check the GenerateINSERT, UPDATE, and DELETE statements checkbox (see Figure 11).

 


Figure 11: Advanced SQL generationoptions.

 

Next, drag and drop a DetailsView control on the Web formand set its DataSourceID property to SqlDataSource1. From the smart tag of theDetailsView enable paging, inserting, editing, and deleting. Note that Figure 9shows a link titled Send Invitations, along with Edit, New, and Delete links.This is used to send invitations to all the participants of a current survey. Toadd this button open the Fields dialog box of DetailsView, select the CommandField, and click the Convert this field into a TemplateField button. This willconvert the command field into a template field; we can add the SendInvitations button there. Then right click on the DetailsView and select EditTemplate, then select Field[3]. This will open the template designer, as shownin Figure 12.

 


Figure 12: Adding the SendInvitations button.

 

Drag and drop a LinkButton in the ItemTemplate beside theDelete button and set its Text property to Send Invitations. Also, set itsCommandName property to Invite. This way we ll be able to identify it in ourcode. Finally, drag and drop a Label control below the DetailsView and set itsEnableViewState property to False and its Text property to an empty string.This label will be used to display a success message after sending theinvitations. Listing One shows the complete markup ofManageSurvey.aspx.

 

Conclusion

In Part I of this three-part series we gathered thefunctional requirements for the survey application and designed the databaseschema. We also began developing administrative Web forms by creating a masterpage and a Web form for managing survey definitions. In Part II we ll develop Webforms for managing survey questions and their choices. We ll also develop a Webform for managing survey participants.

 

The sample code forthis series is available for download.

 

Bipin Joshi is thefounder and owner of BinaryIntellect Consulting (http://www.binaryintellect.com),where he conducts professional training programs on .NET technologies. He isthe author of Developer s Guide to ASP.NET 2.0(http://www.binaryintellect.com/books)and co-author of three WROX books on .NET 1.x. He writes regularly for http://www.DotNetBips.com, a communityWeb site he founded in the early days of .NET. He is a Microsoft MVP, MCAD,MCT, and member of ASPInsiders. He jots down his thoughts about .NET, life, andYoga at http://www.bipinjoshi.com. Healso conducts workshops on Yoga and Meditation, where he helps IT professionalsdevelop a positive personality. You can contact him at mailto:[email protected].

 

Begin Listing One Markup ofManageSurvey.aspx

<%@ Page Language="VB" MasterPageFile="~/Admin/

 AdminMasterPage.master"AutoEventWireup="false"

 CodeFile="ManageSurvey.aspx.vb"Inherits="Admin_

 CreateSurvey"title="Untitled Page" %>

 "ContentPlaceHolder1"Runat="Server">  "DetailsView1"runat="server" AllowPaging="True"  AutoGenerateRows="False" CellPadding="4"  DataKeyNames="SurveyID"DataSourceID="SqlDataSource1"  ForeColor="#333333" GridLines="None"Height="50px"  Width="100%">  ForeColor="White" />  HorizontalAlign="Center" />  InsertVisible="False" ReadOnly="True"  SortExpression="SurveyID" />  SortExpression="Title" />  SortExpression="Description">  '<%#Bind("Description") %>' TextMode="MultiLine">    Text='<%#Bind("Description") %>' TextMode="MultiLine">    Text='<%#Bind("Description") %>'>  CausesValidation="True"CommandName="Update" Text="Update">  CausesValidation="False"CommandName="Cancel" Text="Cancel">  CausesValidation="True"CommandName="Insert" Text="Insert">  CausesValidation="False"CommandName="Cancel" Text="Cancel">  CausesValidation="False"CommandName="Edit" Text="Edit">  CausesValidation="False"CommandName="New" Text="New">  CausesValidation="False"CommandName="Delete" Text="Delete">  CommandName="Invite">SendInvitations  Font-Bold="True"Width="20%" />  ForeColor="White" />  ForeColor="#284775" />  Font-Bold="True"ForeColor="Red">
 ConnectionString="<%$ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Survey] WHERE [SurveyID] = @SurveyID"InsertCommand="INSERT INTO [Survey] (Develop a Survey Application: Part I,   [Description]) VALUES(@Title, @Description)" SelectCommand="SELECT * FROM [Survey]"  UpdateCommand="UPDATE[Survey] SET Develop a Survey Application: Part I = @Title,   [Description] =@Description WHERE [SurveyID] = @SurveyID">

End Listing One

 

 

 

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like