Click here to Skip to main content
Click here to Skip to main content

Survey/Polling System Using RadioButton List

, 9 May 2012
Rate this:
Please Sign up or sign in to vote.
An article to demonstrate a polling/survey system in ASP.NET using a RadioButtonList.

Poll

Poll

Introduction

Here I show a simple way to develop a polling/survey system like that in CodeProject. It shows the results with percentage of votes in a graphical manner. I have used a RadioButtonList to show poll options to select an option.

Database Diagram

I have three tables: one for question, second for offered answers, and third to store the answers selected by users.

Insert Poll and Options

Here is the Stored Procedure for inserting a poll and various options/answers:

CREATE PROCEDURE [dbo].[insQues]
    
    @ques varchar(350),
    @other varchar(250)
    as
BEGIN
        SET NOCOUNT ON;
    update tblQues set status=0;
    insert into tblQues (qText,other) values(@ques,@other);
END

CREATE PROCEDURE [dbo].[insAnswers]
    
    
    @anstext varchar(450)
    
    as
BEGIN
        SET NOCOUNT ON;
    declare @qid int
    select @qid=MAX(qid) from tblQues;
    insert into tblQuesAns (qID,anstext) values(@qid,@anstext);
END

Here I input options/answers in one text box and split it by ';' to separate various options.

SqlCommand cmdins = ff.getCommand("insQues");
cmdins.Parameters.AddWithValue("@ques", txtQues.Text);
cmdins.Parameters.AddWithValue("@other", txtOther.Text);
cmdins.ExecuteNonQuery();
//we have to insert each answer so split it by ; to seperate answer
 String[] ans = txtAns.Text.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

foreach (string ss in ans)
{
    cmdins = ff.getCommand("insAnswers");
    cmdins.Parameters.AddWithValue("@anstext", ss);
    cmdins.ExecuteNonQuery();
}
cmdins.Connection.Close();

Showing Graphical Results

To show results graphically for quick analysis, I have used a StringBuilder and calculate the percentage as Number of votes of respective answers*100/total no of votes.

And now I set an image within the td and the width of this image is this percentage. See Image-2.

Point of Interest

Here I want to fetch the answers and the total number of answers count from the tblQuesAns table. So I use an output parameter.

CREATE PROC [dbo].[selectResults]
(
    @qid int,
    @b int output
)
AS
BEGIN
select anstext,anscount from tblQuesAns where qID=@qid;
set @b=(select sum(anscount) from tblQuesAns where qID=@qid);

END

Now, how to fetch the value of @b in C#:

SqlCommand cmd = ff.getCommand("selectResults");
cmd.Parameters.AddWithValue("@qid",qid);
SqlParameter sp = cmd.Parameters.Add("@b", SqlDbType.Int);
sp.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int total =int.Parse(cmd.Parameters["@b"].Value.ToString());
SqlDataReader dr = cmd.ExecuteReader();

Future Scope

This has the limitation to select only one option. So I will modify this with a RadioButtonList and CheckBoxList both, and change this to a custom control to make it more useful. Thank you for reading.

License

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

About the Author

Uma Shankar Patel
Software Developer
India India
I have completed MCA(Computer Applications) from Utter Pradesh Technical University,Lucknow.
My areas of interest are database development and application software development using Microsoft Technologies.
Visit my blog: queryingsql.com
 
Blog | Articles | Answers |Facebook
The best anti-virus is a smart user
 
Follow on   Google+

Comments and Discussions

 
Questionnice job Pinmembereenoogsa9-May-12 18:42 
AnswerRe: nice job PinmemberUma Shankar Patel9-May-12 18:44 
thanks for appreciation.......

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 9 May 2012
Article Copyright 2012 by Uma Shankar Patel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid