polls.aspx
<%@ Page Language="VB" EnableViewState="False" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SQLClient" %>
<script language="VB" runat="server">
Dim strScriptName As String
Dim intPollId As Integer
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim myDataReader As SqlDataReader
strScriptName = Request.ServerVariables("URL")
If Request.QueryString("pid") = "" Then
intPollId = 1
Else
intPollId = Request.QueryString("pid")
End If
' Create a new Connection object that connects to our SQL Server.
' It's running on a separate machine located at the IP address
' 10.2.2.133. The name of the database is "samples".
myConnection = New SqlConnection("Data Source=10.2.2.133;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;")
' Open the connection to the database
myConnection.Open
If Request.QueryString("action") = "results" Then
myCommand = New SqlCommand()
myCommand.Connection = myConnection
' Update database with any vote passed in
If Request.QueryString("vote") <> "" Then
myCommand.CommandText = "UPDATE polls SET " _
& "votes" & CInt(Request.QueryString("vote")) _
& " = " _
& "votes" & CInt(Request.QueryString("vote")) _
& " + 1 WHERE id=" & intPollId & ";"
myCommand.ExecuteNonQuery()
End If
' Get info about the poll
myCommand.CommandText = "SELECT * FROM polls WHERE id=" & intPollId & ";"
' Use the ExecuteReader method of the Command object to execute
' our query and return the results via a DataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
' Specify the DataReader as the source of the data for our Repeater
ResultsRepeater.DataSource = myDataReader
ResultsRepeater.DataBind()
Else
' Get info about the poll
myCommand = New SqlCommand("SELECT * FROM polls WHERE id=" & intPollId & ";", myConnection)
' Use the ExecuteReader method of the Command object to execute
' our query and return the results via a DataReader
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
' Specify the DataReader as the source of the data for our Repeater
PollRepeater.DataSource = myDataReader
PollRepeater.DataBind()
End If
' Close our DataReader and Connection
myDataReader.Close()
myConnection.Close()
End Sub
</script>
<html>
<head>
<title>ASP.NET Poll Sample</title>
</head>
<body>
<form runat="server">
<asp:Repeater id="PollRepeater" runat="server">
<ItemTemplate>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td bgcolor="#006600" align="left"><font color="#FFFFFF"><strong>Quick Question:</strong></font></td>
<td bgcolor="#006600" align="right"><a href="<%= strScriptName %>?action=results&pid=<%= intPollId %>"><font color="#FFFFFF" size="-1">View Results</font></a></td>
</tr>
<tr>
<td colspan="2" bgcolor="#009933" align="center"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "question") %></font></td>
</tr>
<tr>
<td colspan="2" bgcolor="#009933" align="center">
<a href="<%= strScriptName %>?action=results&pid=<%= intPollId %>&vote=1"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice1") %></a></font>
<a href="<%= strScriptName %>?action=results&pid=<%= intPollId %>&vote=2"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice2") %></a></font>
<a href="<%= strScriptName %>?action=results&pid=<%= intPollId %>&vote=3"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice3") %></a></font>
<a href="<%= strScriptName %>?action=results&pid=<%= intPollId %>&vote=4"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice4") %></a></font>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater id="ResultsRepeater" runat="server">
<ItemTemplate>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td bgcolor="#006600" align="left"><font color="#FFFFFF"><strong>Poll #<%= intPollId %> Results</strong></font></td>
</tr>
<tr>
<td bgcolor="#009933" align="center"><font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "question") %></font></td>
</tr>
<tr>
<td bgcolor="#009933">
<font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice1") %>:</font> <strong><%# DataBinder.Eval(Container.DataItem, "votes1") %></strong>
<font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice2") %>:</font> <strong><%# DataBinder.Eval(Container.DataItem, "votes2") %></strong>
<font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice3") %>:</font> <strong><%# DataBinder.Eval(Container.DataItem, "votes3") %></strong>
<font color="#FFFFFF"><%# DataBinder.Eval(Container.DataItem, "choice4") %>:</font> <strong><%# DataBinder.Eval(Container.DataItem, "votes4") %></strong>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/polls_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>