Click here to Skip to main content
16,019,618 members
Articles / Web Development / ASP.NET

ASP.NET Simple Survey Application

Rate me:
Please Sign up or sign in to vote.
4.87/5 (18 votes)
24 Jan 2013CPOL4 min read 189.5K   17.3K   58   21
This kind of requirement is useful in many applications originally posted on http://www.srinetinfo.com/search/label/Survey%20Application

Introduction

The companies which are offering variety of services to the customers, they need to get the response from the customers in many forms. By analyzing these responses companies will have an overview of their services and performance.           

The surveys or polls are very much important in different situation and organizations. for examples, consider a company is selling different products in the market. If they want to know the product usage and end user satisfaction they will conduct a simple survey among the users after collecting most of the public responses, they start analyzing these response in different angles like, 

  1. What is usage percentage of this product among the people.
  2. How often people are purchasing this product
  3. Public satisfaction index and etc. 

Background

We have different websites which are providing simple prepare your question and answer sets and publish or share you survey link to the targeted groups, networks or individuals. We can have our own solution embed in our regular applications which will be opened for a duration for the organization people to fill. And finally for common people to share their surveys or polls we have social networking sites offering with limited features
 
We need to develop a simple application where administrators can prepare a questionnaire and prepare a simple survey and share it to the people who ever registered on our website. Once the survey filled by end users, the web site administrator or who ever is authorized can be analyze the survey results, feedback in any form like graphical or textual. 

High Level Design

Survey Application Usecase 

Actors

  • Administrator: The person who prepares the surveys and share it with the registered users via mail.
  • Manager: The person who analyzes and prepares reports on the filled surveys 
  • User: Is the actual and user who fills the survey forms after getting them via mail. 

Actions

  • Register: The user registers to the website initially
  • Create Survey: The administrator creates the survey
  • Share Survey: Once the survey creates Admin shares this survey with end users
  • Fill Survey: End user fills and sends the response.
  • Analyze Surveys: once the survey responses start getting Manage can start analyzing them on different parameters 

Class Diagram

 

DB Schema

simple survey app schema

Using the code 

So far we are done with high level design, DB design and class diagrams. Now we see how the actual application development developed step by step.

Create Database 

Create a new database with the name SurveyApp as shown in the figure

sql server database screen

Create new tables with the following script on this database 

SQL
USE [SurveyApp]
GO
/****** Object:  Table [dbo].[Roles]    Script Date: 09/12/2012 15:46:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Roles](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Name] [varchar](200) NOT NULL,
 CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
    IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Questions]    Script Date: 09/12/2012 15:46:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Questions](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Text] [varchar](200) NOT NULL,
 [QuestionType] [varchar](200) NOT NULL,
 [Options] [varchar](2000) NOT NULL,
 CONSTRAINT [PK_Questions] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
   IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Users]    Script Date: 09/12/2012 15:46:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Users](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [FirstName] [varchar](200) NOT NULL,
 [LastName] [varchar](200) NULL,
 [UserName] [varchar](200) NOT NULL,
 [Password] [varchar](200) NOT NULL,
 [Role] [int] NOT NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
   IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Surveys]    Script Date: 09/12/2012 15:46:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Surveys](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Title] [varchar](200) NULL,
 [Description] [varchar](200) NOT NULL,
 [CreatedOn] [datetime] NOT NULL,
 [ExpiresOn] [datetime] NULL,
 [CreatedBy] [int] NOT NULL,
 [Publish] [bit] NOT NULL,
 CONSTRAINT [PK_Surveys] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, 
      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[SurveyResponse]    Script Date: 09/12/2012 15:46:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[SurveyResponse](
 [ID] [int] NOT NULL,
 [SurveyID] [int] NOT NULL,
 [QuestionID] [int] NOT NULL,
 [Response] [varchar](200) NOT NULL,
 [FilledBy] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Survey_Questions]    Script Date: 09/12/2012 15:46:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Survey_Questions](
 [ID] [int] NOT NULL,
 [SurveyID] [int] NOT NULL,
 [QuestionID] [int] NOT NULL,
 [OrderId] [int] NULL
) ON [PRIMARY]
GO
/****** Object:  ForeignKey [FK_Survey_Questions_Questions]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[Survey_Questions]  WITH CHECK ADD 
      CONSTRAINT [FK_Survey_Questions_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[Survey_Questions] CHECK CONSTRAINT [FK_Survey_Questions_Questions]
GO
/****** Object:  ForeignKey [FK_Survey_Questions_Surveys]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[Survey_Questions]  WITH CHECK ADD  
      CONSTRAINT [FK_Survey_Questions_Surveys] FOREIGN KEY([SurveyID])
REFERENCES [dbo].[Surveys] ([ID])
GO
ALTER TABLE [dbo].[Survey_Questions] CHECK CONSTRAINT [FK_Survey_Questions_Surveys]
GO
/****** Object:  ForeignKey [FK_SurveyResponse_Questions]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[SurveyResponse]  WITH CHECK ADD 
      CONSTRAINT [FK_SurveyResponse_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[SurveyResponse] CHECK CONSTRAINT [FK_SurveyResponse_Questions]
GO
/****** Object:  ForeignKey [FK_SurveyResponse_Surveys]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[SurveyResponse]  WITH CHECK ADD 
      CONSTRAINT [FK_SurveyResponse_Surveys] FOREIGN KEY([SurveyID])
REFERENCES [dbo].[Surveys] ([ID])
GO
ALTER TABLE [dbo].[SurveyResponse] CHECK CONSTRAINT [FK_SurveyResponse_Surveys]
GO
/****** Object:  ForeignKey [FK_SurveyResponse_Users]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[SurveyResponse]  WITH CHECK ADD  
      CONSTRAINT [FK_SurveyResponse_Users] FOREIGN KEY([FilledBy])
REFERENCES [dbo].[Users] ([ID])
GO
ALTER TABLE [dbo].[SurveyResponse] CHECK CONSTRAINT [FK_SurveyResponse_Users]
GO
/****** Object:  ForeignKey [FK_Surveys_Users]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[Surveys]  WITH CHECK ADD  CONSTRAINT [FK_Surveys_Users] FOREIGN KEY([CreatedBy])
REFERENCES [dbo].[Users] ([ID])
GO
ALTER TABLE [dbo].[Surveys] CHECK CONSTRAINT [FK_Surveys_Users]
GO
/****** Object:  ForeignKey [FK_Users_Roles]    Script Date: 09/12/2012 15:46:11 ******/
ALTER TABLE [dbo].[Users]  WITH CHECK ADD  CONSTRAINT [FK_Users_Roles] FOREIGN KEY([Role])
REFERENCES [dbo].[Roles] ([ID])
GO
ALTER TABLE [dbo].[Users] CHECK CONSTRAINT [FK_Users_Roles]
GO 

Create Project

  1. On Visual studio select new project 
  2. Select ASP.NET web application template
  3. Name the project as SimpleSurvey

New Project

Remove all default created folder as shown in the below figure 

Survey Project

Add new form to the project with the name SurveyForm.aspx

New Survey Form

Add a new class with name SurveysManager.cs 

Survey Manager Form

Add an enumeration to the application so that we can easily categorize questions 

Add the following enumeration to SurveysManager.cs 

C#
public enum QuestionTypes
{
    SingleLineTextBox, // will render a textbox 
    MultiLineTextBox, // will render a text area
    YesOrNo, //will render a checkbox
    SingleSelect, //will render a dropdownlist
    MultiSelect //will render a listbox
} 

Entity Framework for DB Interface

Here we are going to use EF for DB related actions. Just add a new file with the name SurveyAppContext.edmx file as shown below.

Add new item of ADO.NET Entity Model from visual studio data templates.

Survey Application

Choose select Generate from database model to proceed 

Data Model

Choose a connection string and proceed 

Database Connection String

Select the tables required for db activities and finish 

Database tables

Finally the Entity Framework will generate the model in visual studio as below and the it generates necessary methods to perform CRUD operations on entities. This you can check on below images.

Survey Application Model

Model 

Survey Application Project Hieraurchy

Create A new form to Manage Questions that should like as below. 

Manage Questions

Create a new form to manage surveys that should like as below 

Manage Survey

We are done with creating the Manage Questions and Manage Surveys screens. Now we are half done with the requirement. These two will provide us the way to add few questions and surveys.

We take a simple example of User feedback form for product.

The feedback should consists of the following Question.

  1. First Name
  2. Last Name
  3. User Email ID
  4. Mobile Number (Optional)
  5. Rating (1 to 5)
  6. Comments

And the survey title should be <XXXX> Feedback Form

Add all the questions from the Manage Questions Screen.

Survey Application

Add feedback survey from Manage Survey Screen. 

Survey Application

As shown in the above step add all the questions and survey to the database.  

Feedback Form Rendering 

The actual part of the application now we will discuss. 

Step - I Fetch the respective Survey definition from the database:

 Our render page will be like this and will list number of surveys added already. Select to render the sample feedback form.

Feedback Form

Finally once you selected the survey you on drop down list. It will post back and render the page with questions and respective fields as shown in the figure below.

Survey Form

The basic code format is here how to render the page with respective controls.  

C#
private void PopulateSurvey()
{
    List<Question> questions = (from p in context.Questions
                                join q in context.SurveyQuestions on p.ID equals q.QuestionID
                                where q.SurveyID == surveyid
                                select p).ToList();
    Table tbl = new Table();
    tbl.Width = Unit.Percentage(100);
    TableRow tr;
    TableCell tc;
    TextBox txt;
    CheckBox cbk;
    DropDownList ddl;
    foreach (Question q in questions)
    {
        tr = new TableRow();
        tc = new TableCell();
        tc.Width = Unit.Percentage(25);
        tc.Text = q.Text;
        tc.Attributes.Add("id", q.ID.ToString());
        tr.Cells.Add(tc);
        tc = new TableCell();
        if (q.QuestionType.ToLower() == "singlelinetextbox")
        {
            txt = new TextBox();
            txt.ID = "txt_" + q.ID;
            txt.Width = Unit.Percentage(40);
            tc.Controls.Add(txt);
        }
        if (q.QuestionType.ToLower() == "multilinetextbox")
        {
            txt = new TextBox();
            txt.ID = "txt_" + q.ID;
            txt.TextMode = TextBoxMode.MultiLine;
            txt.Width = Unit.Percentage(40);
            tc.Controls.Add(txt);
        }
        if (q.QuestionType.ToLower() == "singleselect")
        {
            ddl = new DropDownList();
            ddl.ID = "ddl_" + q.ID;
            ddl.Width = Unit.Percentage(41);
            if (!string.IsNullOrEmpty(q.Options))
            {
                string[] values = q.Options.Split(',');
                foreach (string v in values)
                    ddl.Items.Add(v.Trim());
            }
            tc.Controls.Add(ddl);
        }
        tc.Width = Unit.Percentage(80);
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);
    }
    pnlSurvey.Controls.Add(tbl);
}

Below is the code to get response from the dynamic controls, after the submit button is clicked.

C#
private List<Survey_Response> GetSurveyReponse()
{
    List<Survey_Response> response = new List<Survey_Response>();
    foreach (Control ctr in pnlSurvey.Controls)
    {
        if (ctr is Table)
        {
            Table tbl = ctr as Table;
            foreach (TableRow tr in tbl.Rows)
            {
                Survey_Response sres = new Survey_Response();
                sres.FilledBy = 2;
                sres.SurveyID = surveyid;
                sres.QuestionID = Convert.ToInt32(tr.Cells[0].Attributes["ID"]);
                TableCell tc = tr.Cells[1];
                foreach (Control ctrc in tc.Controls)
                {
                    if (ctrc is TextBox)
                    {
                        sres.Response = (ctrc as TextBox).Text.Trim();
                    }
                    else if (ctrc is DropDownList)
                    {
                        sres.Response = (ctrc as DropDownList).SelectedValue;
                    }
                    else if (ctrc is CheckBox)
                    {
                        sres.Response = (ctrc as CheckBox).Checked.ToString();
                    }
                }
                response.Add(sres);
            }
        }
    }
    return response;
}

This is simple application and can be extended to get more features and can include more functionality to get more live functionality. Working code can be downloaded here.

Points of Interest 

This covers the following concepts like

Dynamic Controls, Dynamic Forms,  Form Feedbacks, and the generalization of the feature requirements 

History

Initial version published on 31-October-2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralNot for Newbie Pin
dotvbnet5-Jun-18 19:42
dotvbnet5-Jun-18 19:42 
QuestionVery Nice Article Pin
Member 1132753225-Apr-18 14:57
Member 1132753225-Apr-18 14:57 
QuestionI get error when build the project Pin
phamxuannguyen5-Oct-17 22:34
phamxuannguyen5-Oct-17 22:34 
AnswerRe: I get error when build the project Pin
Juan M Uberti24-Nov-17 10:00
Juan M Uberti24-Nov-17 10:00 
QuestionError in ManageSurveys.aspx Pin
Nehru Gunter14-Mar-17 16:44
Nehru Gunter14-Mar-17 16:44 
GeneralDrawbacks in this application Pin
sanprets17-Nov-15 21:17
sanprets17-Nov-15 21:17 
QuestionSubmit ManageSurveys - ERROR Pin
Member 1189036416-Sep-15 4:02
Member 1189036416-Sep-15 4:02 
QuestionQuestion Pin
Christian Winters3-Nov-14 5:14
Christian Winters3-Nov-14 5:14 
QuestionReporting Pin
vdykcj19-Oct-14 21:41
vdykcj19-Oct-14 21:41 
AnswerRe: Reporting Pin
Srinivasu Pemma30-Oct-14 20:54
Srinivasu Pemma30-Oct-14 20:54 
QuestionType or Namespace not found? Pin
Jethro May11-Sep-14 20:30
Jethro May11-Sep-14 20:30 
QuestionNice Article! Pin
Member 1017299723-Dec-13 6:47
Member 1017299723-Dec-13 6:47 
QuestionError when Submit in ManageSurveys Pin
tran123423-Oct-13 3:33
tran123423-Oct-13 3:33 
AnswerRe: Error when Submit in ManageSurveys Pin
Member 839363530-Jun-14 22:10
Member 839363530-Jun-14 22:10 
AnswerRe: Error when Submit in ManageSurveys Pin
Member 83936351-Jul-14 1:23
Member 83936351-Jul-14 1:23 
GeneralRe: Error when Submit in ManageSurveys Pin
Nehru Gunter9-Mar-17 18:17
Nehru Gunter9-Mar-17 18:17 
QuestionGetting error Pin
RohanPrasanna22-Sep-13 3:52
RohanPrasanna22-Sep-13 3:52 
AnswerRe: Getting error Pin
Member 103210849-Oct-13 10:29
Member 103210849-Oct-13 10:29 
GeneralRe: Getting error Pin
Srinivasu Pemma10-Oct-13 4:38
Srinivasu Pemma10-Oct-13 4:38 
QuestionSurvey Issue Pin
eddieli822-May-13 11:39
eddieli822-May-13 11:39 
GeneralMy vote of 5 Pin
Unque25-Jan-13 5:26
Unque25-Jan-13 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.