Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Survey/Polling System Using RadioButton List

4.86/5 (6 votes)
9 May 2012CPOL1 min read 27.6K   2.2K  
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)