65.9K
CodeProject is changing. Read more.
Home

Survey/Polling System Using RadioButton List

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.86/5 (6 votes)

May 9, 2012

CPOL

1 min read

viewsIcon

27550

downloadIcon

2186

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.