Simple ASP.NET Survey Application
Introduction
Companies offering a variety of services to customers need to get responses from customers in many ways. By analyzing these responses companies will have an overview of their services and performance.
The surveys or polls are very much important in various situations and organizations. For example, consider a company is selling various 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 responses in various way, like:
- What is the percentage of usage of this product among the people.
- How often are people purchasing this product.
- Public satisfaction index, etc.
All these will be helpful in:
- Promoting their business to another level.
- Analyze the future market.
- Adjust production depending on market needs.
Solution
We have various websites providing simple question and answer sets for you to publish or share your survey link to the targeted groups, networks or individuals.
We can have our own solution embeded in our regular applications which will be opened for a duration for the organization people to complete.
And finally for common people to share their surveys or polls we have social networking sites offering limited features.
Problem Statement
We need to develop a simple application where administrators can prepare a questionnaire and prepare a simple survey and share it to the people, whoever registered on our website.
Once the survey is completed by end users, the web site administrator (or whoever is authorized) can analyze the survey results and other feedback in any form like graphical or textual.
This example has the following requirements.
Consider we have a requirement to conduct a survey of customer's travel experiences for tickets booked from an online reservation system that books tickets from various travel agencies.
High Level Design
Actors
- Administrator: The person who prepares the surveys and shares it with the registered users via mail.
- Manager: The person who analyzes and prepares reports on the completed surveys
- User: Is the actual end user who completes 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 has been created, Admin shares this survey with end users
- Complete Survey: End user completes and sends the response.
- Analyze Surveys: once the survey responses begin to arrive, Manage can start analyzing them using various parameters
Class Diagrams
DB Schema
The Schema is very simple and indicates the relation among all the entities in the application.
We are now done with the High Level Design, DB Design and Class diagrams. Now we see how the actual application is developed step-by-step.
Create Database
Create a new database with the name SurveyApp as shown in the figure:
Create new tables with the following script in this database:
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
- In Visual Studio select new project
- Select ASP.Net web application template
- Name the project as SimpleSurvey
Remove all the folders, which have been created by default, as shown in the following figure:
Add new form to the project with the name SurveyForm.aspx, as in:
Add a new class with the name SurveysManager.cs, as in:
Add an enumeration to the application so that we can easily categorize questions.
Add the following enumeration to SurveysManager.cs:
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 the Entity Framework for the DB related actions. Just add a new file with the name SurveyAppContext.edmx as shown below.
Add a new item of the ADO.NET Entity Model from the Visual Studio data templates.
Select Generate from the database model to proceed; see:
Choose a connection string and proceed; see:
Select the tables required for db activities and finish; see:
Finally the Entity Framework will generate the model in Visual Studio as in the following and then it generates the necessary methods to perform CRUD operations on entities. This you can see in the following images.
Model
Create A new form to Manage Questions such as in the following:
Create a new form to manage surveys such as in the following:
We are done with creating the Manage Questions and Manage Surveys screens. Now we are half done with the requirements. These two will provide us a way to add a few questions and surveys.
We take a simple example of User feedback form for a product.
The feedback should consist of the following questions:
- First Name
- Last Name
- User Email ID
- Mobile Number (Optional)
- Rating (1 to 5)
- Comments
And the survey title should be <XXXX> Feedback Form.
Add all the questions from the Manage Questions Screen.
Add the feedback survey from the Manage Survey Screen.
As shown in the above step, add all the questions and surveys to the database.
Feedback Form Rendering
We will now discuss the actual part of the application now.
Step 1
Fetch the respective Survey definition from the database.
Fetch the respective Survey definition from the database.
Our render page will be like this and will list a number of surveys added already. Select to render the sample feedback form.
Finally, once you select the survey from the drop down list, it will post back and render the page with the questions and respective fields as shown in the figure below.
The basic code format is here, how to render the page with respective controls.
private void PopulateSurvey()
{
List<Question> questions = (from p in context.Questions
join q in context.SurveyQuestionson 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 qin questions)
{
tr = newTableRow();
tc = newTableCell();
tc.Width = Unit.Percentage(25);
tc.Text = q.Text;
tc.Attributes.Add("id", q.ID.ToString());
tr.Cells.Add(tc);
tc = newTableCell();
if (q.QuestionType.ToLower() == "singlelinetextbox")
{
txt = newTextBox();
txt.ID = "txt_" + q.ID;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
if (q.QuestionType.ToLower() == "multilinetextbox")
{
txt = newTextBox();
txt.ID = "txt_" + q.ID;
txt.TextMode = TextBoxMode.MultiLine;
txt.Width = Unit.Percentage(40);
tc.Controls.Add(txt);
}
if (q.QuestionType.ToLower() == "singleselect")
{
ddl = newDropDownList();
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);
}
The following is the code to get responses from the dynamic controls, after the submit button is clicked.
private List<Survey_Response> GetSurveyReponse()
{
List<Survey_Response> response =new List<Survey_Response>();
foreach (Control ctr in pnlSurvey.Controls)
{
if (ctr isTable)
{
Table tbl = ctras 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 isTextBox)
{
sres.Response = (ctrcas TextBox).Text.Trim();
}
else if (ctrcis DropDownList)
{
sres.Response = (ctrcas DropDownList).SelectedValue;
}
else if (ctrcis CheckBox)
{
sres.Response = (ctrcas CheckBox).Checked.ToString();
}
}
response.Add(sres);
}
}
}
return response;
}
Originally Posted At: http://www.srinetinfo.com
No comments:
Post a Comment