ASP.NET Poll Control In VB.NET

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:

  1. Open your Visual Studio and create a new empty ASP.NET website (VB).

  2. Add new item, select webform, and name it default.aspx. Try to make something like this:



    If you didn’t here is the code,



  3. Add another webform and call it results.aspx, like this:


    And here is the code to understand.



  4. Now, add a SQL database and name it userpolls.mdf.

  5. Add tables.



  6. Add a new folder to the website and name it as Bin. Paste the pollcontroll.dll there along with pollcontroll.pdb.

  7. Make your web.config file similar to this.



  8. Add another folder and name it as images. Include all the images, in the folder, that you have downloaded or created on your own.

  9. Double click on default.aspx and paste this code there.
    1. Imports System.Data  
    2. Imports System.Data.SqlClient  
    3. Partial Class _Default  
    4. Inherits System.Web.UI.Page  
    5. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
    6. Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)  
    7. Dim cmd As SqlCommand  
    8. Dim cmdtext As String  
    9. Dim obj As SqlDataReader  
    10. If Not Me.Page.IsPostBack Then  
    11. Pollcontrol1.CanVote = True ' Add Question Text To Poll Control  
    12. cmdtext = "select QuestionText from PollQuestions where Iscurrent=1 and Isarchived=0"  
    13. cmd = New SqlCommand(cmdtext, connection)  
    14. connection.Open()  
    15. Pollcontrol1.PollQuestion = cmd.ExecuteScalar  
    16. connection.Close()  
    17. cmdtext = "select optionID,PollID,OptionText,Votes from PollOptions where pollID in(select PollID from Pollquestions where Iscurrent=1 and Isarchived=0)"  
    18. cmd = New SqlCommand(cmdtext, connection)  
    19. connection.Open()  
    20. obj = cmd.ExecuteReader ' Add Options To Poll Control :  
    21. While obj.Read  
    22. Pollcontrol1.AddPollAnswer(obj("pollID"), obj("optionID"), obj("optionText"), obj("votes"))  
    23. End While  
    24. connection.Close()  
    25. obj.Close()  
    26. End If  
    27. End Sub  
    28. Protected Sub Pollcontrol1_CastVote(ByVal PollId As Integer, ByVal AnswerId As String, ByVal MemberId As Integer) Handles Pollcontrol1.CastVote ' Update PollOptions Without Check MemberID  
    29. Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)  
    30. Dim com As New SqlCommand("update pollOptions set votes=votes+'1' where OptionID=@optionID", connection)  
    31. connection.Open()  
    32. com.Parameters.Add("@optionID", SqlDbType.Int).Value = Int(AnswerId)  
    33. com.ExecuteNonQuery()  
    34. connection.Close()  
    35. Response.Redirect("result.aspx")  
    36. End Sub  
    37. End Class  
  10. Now, do the same with result.aspx and paste the following code.
    1. Imports System.Data  
    2. Imports System.Data.SqlClient  
    3. Partial Class result  
    4. Inherits System.Web.UI.Page  
    5. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
    6. Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("UserPolls").ToString)  
    7. Dim cmd As SqlCommand  
    8. Dim cmdtext As String  
    9. Dim obj As SqlDataReader  
    10. If Not Me.Page.IsPostBack Then  
    11. Pollcontrol1.CanVote = False  
    12. cmdtext = "select optionID,PollID,OptionText,Votes from PollOptions where pollID in(select PollID from Pollquestions where Iscurrent=1 and Isarchived=0)"  
    13. cmd = New SqlCommand(cmdtext, connection)  
    14. connection.Open()  
    15. obj = cmd.ExecuteReader ' Add Options To Poll Control :  
    16. While obj.Read  
    17. Pollcontrol1.AddPollAnswer(obj("pollID"), obj("optionID"), obj("optionText"), obj("votes"))  
    18. End While  
    19. connection.Close()  
    20. obj.Close()  
    21. End If  
    22. End Sub  
    23. End Class  
  11. Add stylesheet from the folder.

  12. Now, run it and enjoy.
I hope this helped you. Comment for questions!!!

Up Next
    Ebook Download
    View all
    Learn
    View all