Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET
Tip/Trick

Survey/Polling System Using RadioButton List

Rate me:
Please Sign up or sign in to vote.
4.86/5 (6 votes)
9 May 2012CPOL1 min read 26.9K   2.2K   4   4
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.

Image 3

Insert Poll and Options

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

SQL
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.

C#
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.

SQL
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#:

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)


Written By
Software Developer (Senior)
India India

A technology enthusiast and software developer by profession. He is developing .Net,database and azure based enterprise applications from past 6 years.


His skills includes C# ,ASP.NET, ASP.NET MVC,WCF,JQuery,Javascript,SQL Server,Oracle . His areas of interests are database development and application software development using Microsoft Technologies.


Visit his blog: queryingsql.com

Blog | Articles | Answers |Facebook
The best anti-virus is a smart user


Comments and Discussions

 
Questionquestion in polling/survey system Pin
Member 1341267415-Sep-17 20:09
Member 1341267415-Sep-17 20:09 
AnswerRe: question in polling/survey system Pin
uspatel24-Oct-17 0:15
professionaluspatel24-Oct-17 0:15 
Questionnice job Pin
eenoogsa9-May-12 18:42
eenoogsa9-May-12 18:42 
AnswerRe: nice job Pin
uspatel9-May-12 18:44
professionaluspatel9-May-12 18:44 
thanks for appreciation.......

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.