I want to start my C-sharp corner journey with a VB.NET article. Today, I am showing you how to create a simple polling (voting) system in ASP.NET.
Note: I have included the sample of the project so you can download it. After you have opened it, copy all the items and paste in your empty ASP.NET(VB) website. You can paste it in your own website also.
Let’s start it, step by step:
- Open your Visual Studio and create a new empty ASP.NET website (VB).
- Add new item, select webform, and name it default.aspx. Try to make something like this:
If you didn’t here is the code,
- Add another webform and call it results.aspx, like this:
And here is the code to understand.
- Now, add a SQL database and name it userpolls.mdf.
- Add tables.
Add a new folder to the website and name it as Bin. Paste the pollcontroll.dll there along with pollcontroll.pdb.
- Make your web.config file similar to this.
- Add another folder and name it as images. Include all the images, in the folder, that you have downloaded or created on your own.
- Double click on default.aspx and paste this code there.
- Imports System.Data
- Imports System.Data.SqlClient
- Partial Class _Default
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)
- Dim cmd As SqlCommand
- Dim cmdtext As String
- Dim obj As SqlDataReader
- If Not Me.Page.IsPostBack Then
- Pollcontrol1.CanVote = True ' Add Question Text To Poll Control
- cmdtext = "select QuestionText from PollQuestions where Iscurrent=1 and Isarchived=0"
- cmd = New SqlCommand(cmdtext, connection)
- connection.Open()
- Pollcontrol1.PollQuestion = cmd.ExecuteScalar
- connection.Close()
- cmdtext = "select optionID,PollID,OptionText,Votes from PollOptions where pollID in(select PollID from Pollquestions where Iscurrent=1 and Isarchived=0)"
- cmd = New SqlCommand(cmdtext, connection)
- connection.Open()
- obj = cmd.ExecuteReader ' Add Options To Poll Control :
- While obj.Read
- Pollcontrol1.AddPollAnswer(obj("pollID"), obj("optionID"), obj("optionText"), obj("votes"))
- End While
- connection.Close()
- obj.Close()
- End If
- End Sub
- Protected Sub Pollcontrol1_CastVote(ByVal PollId As Integer, ByVal AnswerId As String, ByVal MemberId As Integer) Handles Pollcontrol1.CastVote ' Update PollOptions Without Check MemberID
- Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)
- Dim com As New SqlCommand("update pollOptions set votes=votes+'1' where OptionID=@optionID", connection)
- connection.Open()
- com.Parameters.Add("@optionID", SqlDbType.Int).Value = Int(AnswerId)
- com.ExecuteNonQuery()
- connection.Close()
- Response.Redirect("result.aspx")
- End Sub
- End Class
- Now, do the same with result.aspx and paste the following code.
- Imports System.Data
- Imports System.Data.SqlClient
- Partial Class result
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)
- Dim cmd As SqlCommand
- Dim cmdtext As String
- Dim obj As SqlDataReader
- If Not Me.Page.IsPostBack Then
- Pollcontrol1.CanVote = False
- cmdtext = "select optionID,PollID,OptionText,Votes from PollOptions where pollID in(select PollID from Pollquestions where Iscurrent=1 and Isarchived=0)"
- cmd = New SqlCommand(cmdtext, connection)
- connection.Open()
- obj = cmd.ExecuteReader ' Add Options To Poll Control :
- While obj.Read
- Pollcontrol1.AddPollAnswer(obj("pollID"), obj("optionID"), obj("optionText"), obj("votes"))
- End While
- connection.Close()
- obj.Close()
- End If
- End Sub
- End Class
- Add stylesheet from the folder.
- Now, run it and enjoy.
I hope this helped you. Comment for questions!!!