Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Build a Poll System in ASP.NETIntroductionThere are many websites that have polls on their front page where users (anonymous and members) give their opinions. You should also have noticed that although you are an anonymous user, you are not allowed to vote more than once. In this article, we will look at how to design a polling system, having a back-end to insert new polls along with their answers as well as how to process and store the votes of the users. We will be using stored procedures extensively for interacting with MS SQL 2005 tables. You can download complete source code for this project here Building blocksThere are many approaches to problem solving. One of them is the bottom-up approach. That is, we will begin to design the tables, write down the stored procedures that will interact with the tables and finally the remaining UI logic. FunctionalitiesThe functionalities that are to be implemented are: 2) Displaying the current active poll. 3) Accept a vote from the user and recording the vote. Table schemaWe shall add a database (.mdf file) directly to our solution for simplicity. So, open Visual Web Developer 2005 Express edition and create a new website and call it "Polls". To add the SQL database file, right click on the project and click on "Add New Item". Then select "SQL Database" and name it "Poll.mdf".
Next comes the definition of the tables as shown in the diagrams below.
Stored ProceduresStored procedures will be used to implement the functionalities discussed earlier. Right click on the Stored Procedures subfolder and select "Add New Stored Procedure". 1) Putting up an active pollThe algorithm to put an active poll is to first set all the polls as inactive and then insert the active one in the Polls table, as shown in the stored procedure below.
ALTER PROCEDURE
dbo.NewPoll
DECLARE
@i_NextQuestionID
INT The input parameter @v_Question is the text describing the question of the poll. Secondly, the options for the poll need to be linked with the question itself. This is done by the following store procedure, using the id of the active poll.
ALTER PROCEDURE
dbo.SetPollOption @v_option holds description of the option 2) Displaying the current active poll.The following stored procedure will retrieve the current active poll as well as the options linked with the poll. The stored procedure will return a dataset containing 2 tables, based on the 2 SELECT statement, the first containing the question and the second table containing the options.
ALTER PROCEDURE
dbo.GetActivePoll RETURN 3) Recording a voteRecording a vote is simple. The only thing that has to be done is to increment the number of votes for the option that was selected. The stored procedure below illustrates how this is done.
ALTER PROCEDURE
dbo.IncrementVote The stored procedure takes as parameter the option of the poll that was selected. The logic of preventing more than 1 time vote per poll will be explained later in this article. Designing the User Interface and ImplementationBelow is the screen design where the poll would be input. The various options will be separated by the enter key and processed accordingly.
Clicking on the "Update Poll" button will set the poll as the active one with all the options. The following code illustrates the idea.
Protected
Sub btnUpdatePoll_Click(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnUpdatePoll.Click Firstly, the question is added to the table and then the options, in that order.
Private
Sub addPollQuestion() Finally the function addPollOptions is called to add one by one the options to the table.
Private
Sub addPollOptions(ByVal
strOption As String) The screen below is the voting screen that allows the user to select an option for the poll and click on the vote button.
To display the poll, a label and a radiobuttonlist are used. The label"s text is bounded with the question and the radiobuttonlist bounded to the options, both obtained from the stored procedure "GetActivePoll". The following code snippets do the job.
Private
Function getActivePoll()
As DataSet Next, the poll is bounded with the label and the radiobuttonlist.
Private
Sub DisplayPoll() When the user clicks on the vote button, his vote will be either considered or rejected based on whether he already voted previously. We will make use of cookies to store whether he already voted or not. The property "expires" holds how long the cookie will be stored on hard disk before being expired.
Protected
Sub btnVote_Click(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles btnVote.Click If the vote is considered as a first time vote, the cookie is updated and the number of votes for that option incremented as illustrated by the function below. The id of the option is passed in the stored procedure and is obtained from rdoPollOptionList.SelectedValue.
Private
Sub RecordVote() SummaryIn short, this tutorial explained about using stored procedures, cookies and binding data to various controls. Extensions to the current applicationBased on this example, many things can be achieved: live polls on websites, questionnaires etc... It is up to you to let your imagination flow. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | |