CodeTalk
LANGUAGES: VB.NET
ASP.NET VERSIONS: 2.0
Develop a Survey Application: Part I
Getting Started
By Bipin Joshi
In today s competitive environment companies find it necessary to get feedback on their products and services. Companies also need to know the market trends, as well as customer likes and dislikes. Companies often capture this vital information by conducting surveys about their products and services. A flexible Web-based survey system plays a major role in such scenarios. In this series we are going to develop a survey application using ASP.NET 2.0, VS.NET 2005, and SQL Server 2005. We ll kick things off in Part I with a look at the functional requirements and database schema, then create the survey definitions.
Functional Requirements
Because a survey application captures various pieces of information from the end user it must be flexible and easy to use. A survey consists of questions and feedback or answers to those questions. The answers can be the objective type or in the form of comments. The application we are going to develop will satisfy the following functional requirements:
- The administrator should be able to create a survey definition in the system.
- Each survey consists of a set of questions. There should be Web pages that allow for the addition/modification/deletion of questions 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 user should be presented with radio buttons, checkboxes, or multi-line textboxes at the time of taking the survey.
- Each survey has certain participants. There should be a Web page for managing participants and their e-mail addresses.
- Once a survey is ready with its questions and participants the administrator should be able to send an invitation to all the participants via e-mail. The invitation will consist of a URL wherein the user can take the survey.
- Once the end user goes to the survey URL he should see all the questions from the survey, along with their choices.
- Once the user submits the survey the answers should be saved for generating reports.
- The administrator should be able to see the survey statistics for every objective type (single choice or multiple choice) question.
- The administrative pages should be protected from unauthorized users.
We ll develop this application using ASP.NET 2.0, ADO.NET 2.0, and SQL Server 2005 Express Edition. Specifically, we ll use the following features:
- SQL Data Source control
- Databound controls: GridView, DetailsView, and DataList
- Membership features for securing administrative pages
- TreeView control for navigation
- Sending e-mails
- ADO.NET (SQL Server Data Provider) objects to execute queries wherever required
To begin, create a new ASP.NET Web site using Visual Studio .NET or Visual Web Developer. Make sure to choose a language, such as Visual Basic, and name the site Survey . Once the Web site is created, create a folder named Admin inside the Web site root folder. All the administrative pages will be stored in this folder. The other pages accessible to all the users will be stored directly under the root folder.
Database Schema
All the application data will be stored in a SQL Server 2005 database. To add a new database to the Web site right click on the Web site, select Add New Items, then select SQL Database from the dialog box. Name the database Database1.mdf. Once you have a database in your Web site you can create tables. Figure 1 shows the schema of various tables and Figure 2 lists the 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: Table names and descriptions.
We need to develop seven Web forms and one master page for this 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 Web forms and master pages.
Creating the Master Page
All the administrative pages use a master page named MasterPage.master. This master page provides the navigation tree and overall layout to the other pages. To create this master page, add a new master page in the 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 design
mode.
The master page consists of a TreeView control with nodes pointing to other administrative pages. To design the master page simply drag and drop a TreeView control on the master page. Choose Edit Nodes from its smart tag to open the TreeView Node Editor (see Figure 6). Then add nodes to the TreeView so as to provide navigation structure for all the administrative pages. Set the Text and NavigateUrl properties of various nodes as shown in Figure 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" %>
Figure 8: Markup of 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. The overall layout of the Web form is shown in Figure 9.
Figure 9: Managing the survey
definition.
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 records from the Survey table (see Figure 10).
Figure 10: Configuring a SQL data
source.
Then click the Advanced button and check the Generate INSERT, UPDATE, and DELETE statements checkbox (see Figure 11).
Figure 11: Advanced SQL generation
options.
Next, drag and drop a DetailsView control on the Web form and set its DataSourceID property to SqlDataSource1. From the smart tag of the DetailsView enable paging, inserting, editing, and deleting. Note that Figure 9 shows 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. To add this button open the Fields dialog box of DetailsView, select the Command Field, and click the Convert this field into a TemplateField button. This will convert the command field into a template field; we can add the Send Invitations button there. Then right click on the DetailsView and select Edit Template, then select Field[3]. This will open the template designer, as shown in Figure 12.
Figure 12: Adding the Send
Invitations button.
Drag and drop a LinkButton in the ItemTemplate beside the Delete button and set its Text property to Send Invitations. Also, set its CommandName property to Invite. This way we ll be able to identify it in our code. Finally, drag and drop a Label control below the DetailsView and set its EnableViewState property to False and its Text property to an empty string. This label will be used to display a success message after sending the invitations. Listing One shows the complete markup of ManageSurvey.aspx.
Conclusion
In Part I of this three-part series we gathered the functional requirements for the survey application and designed the database schema. We also began developing administrative Web forms by creating a master page and a Web form for managing survey definitions. In Part II we ll develop Web forms for managing survey questions and their choices. We ll also develop a Web form for managing survey participants.
The sample code for this series is available for download.
Bipin Joshi is the founder and owner of BinaryIntellect Consulting (http://www.binaryintellect.com), where he conducts professional training programs on .NET technologies. He is the 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 community Web 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, and Yoga at http://www.bipinjoshi.com. He also conducts workshops on Yoga and Meditation, where he helps IT professionals develop a positive personality. You can contact him at mailto:[email protected].
Begin Listing One Markup of ManageSurvey.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">Send
Invitations 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