votingstats.aspxReturn to the Documentation page


‘Disable the viewstate at page level to improve the application's performance.’

<%@ Page Language="vb" EnableViewState="false" %>

<%@ import Namespace="System.Data" %>

<%@ import Namespace="System.Data.ODBC" %>

<script runat="server">

‘When the page first loads on the client machine the showPartyVotes function is called – see below.’


‘The showPartyVotes function runs when the page first loads on the client’s computer – it’s called by the Page_Load event. It displays the total number of votes cast for each party in the election.’

‘Declare a string variable [Access2000] to hold the SQL statement SELECT… FROM… GROUP BY… ORDER BY…. This STATEMENT adds up all the votes for each party and sorts them according to who polled the most [Labour – 9,534,459] down to the party that polled the least number of votes [Telepath – 34] – ORDER BY 2 DESC. 2 refers to the second field in the SQL statement which is the sum(votes) calculated field.’

Dim Access2000 as string = "Select party, sum(votes) as partyvotes from votescast group by party order by 2 desc"

‘Declare a variable [MyConn] of type ODBCConnection which references the connection string from the web.config file using the ‘ConfigurationSettings.AppSettings("strConn")’. strConn is the key and its value is the connection string to the database – it’s in the form: "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:uk elections/election2001/database/election2001.mdb;Uid=;Pwd=;". ’

Dim MyConn as New ODBCConnection(ConfigurationSettings.AppSettings("strConn"))

‘Declare a dataset [ds] to hold the returned data.’

Dim ds as DataSet=New DataSet()

‘Declare a new ODBCDataAdapter object [Cmd] with the parameters Access2000 and MyConn. This object pulls the requested data from the election2001 database and puts it into the dataset [ds].’

Dim Cmd as New ODBCDataAdapter(Access2000,MyConn)

‘Use the Fill method to create a table ‘partyvotes’ in the dataset.’


‘Set the datasource property of the Datagrid [gridVotes] to the table ‘partyvotes’ in the dataset.’


‘Bind the data to the web control on the page.’


‘Close the connection object.’


‘The doChangePartyName function is attached to the OnItemDataBound event of the Datagrid web control [gridVotes]. Instead of an abbreviated party name like ‘Con’ it displays a more suitable label – ‘Conservatives’ – and similarly for all the other parties.’

‘Declare a string variable [party] which is assigned to the contents of the first column in the datagrid.’

Dim party as string = e.Item.Cells(0).Text

‘Use a SELECT…CASE…END SELECT coding block to change the party abbreviation to something more suitable.’

select case party

‘When the first column contains ‘Lab’ change the text to ‘Labour’ and likewise for Con, Lib Dem etc….’

Case "Lab"

e.Item.Cells(0).Text = "Labour"

Case "Con"

e.Item.Cells(0).Text = "Conservatives"

Case "Lib Dem"

e.Item.Cells(0).Text = "Liberal Democrats"

Case "SNP"

e.Item.Cells(0).Text = "Scottish Nationalists"

Case "PC"

e.Item.Cells(0).Text = "Plaid Cymru"

Case "UUP"

e.Item.Cells(0).Text = "Ulster Unionists"

Case "DUP"

e.Item.Cells(0).Text = "Democratic Unionists"

Case "UKU"

e.Item.Cells(0).Text = "United Kingdom Unionists"

Case "Ind"

e.Item.Cells(0).Text = "Independents"

Case "UKIP"

e.Item.Cells(0).Text = "UK Independence Party"

Case "Soc All"

e.Item.Cells(0).Text = "Socialist Alliance"

Case "SSP"

e.Item.Cells(0).Text = "Scottish Socialists"

Case "Soc Lab"

e.Item.Cells(0).Text = "Socialist Labour"

Case "BNP"

e.Item.Cells(0).Text = "British Nationalists"

Case "SDLP"

e.Item.Cells(0).Text = "Social & Democratic Labour"

Case "Lib"

e.Item.Cells(0).Text = "Liberals"

Case "Green"

e.Item.Cells(0).Text = "Green Party"

end select

‘The linkToOtherStatsMenu function runs when the user clicks the ASP linkbutton in the top right-hand corner of the screen. It redirects the user back to the Other stats menu.’





<title>Votes cast for the parties</title>

‘The font-size of the Label [Label1] next to the graphic [ballotbox.jpg] is set to 24px’


#Label1 {font-size: 24px}



<body bgcolor="white">

<form id="form1" method="post" runat="server">

‘The table displays an image of a ballot box [ballotbox.jpg] in the top left-hand corner together with a label [Votes cast overall] and the ASP linkbutton control mentioned above displays in the top right-hand corner of the page. This runs the linkToOtherStatsMenu function when clicked.’

<table width="100%">


<td rowspan="2" width="85px" valign="top"><img src="./images/ballotbox.jpg" title="Make voting compulsory!"></td>

<td><asp:Label id="Label1" font-names="Times New Roman" runat="server"><u>Votes cast overall</u></asp:Label></td>

<td align="right"><asp:LinkButton id="LinkToEdit" onclick="linkToOtherStatsMenu" runat="server" Text="Return to the Other stats menu"></asp:LinkButton></td>








<center><H2><asp:Label runat="server" id="partyLabel">The votes cast in June 2001</asp:Label></H2><P>

‘The Datagrid web control [gridVotes] displays the votes cast for every party at the election and ranks them in descending order – highest down to lowest.’

<asp:Datagrid id="gridVotes" Runat="server" OnItemDataBound = "doChangePartyName" Font-Size="16" Headerstyle-HorizontalAlign="center" AutoGenerateColumns="false" BorderColor="Black" AlternatingItemStyle-Font-Size="16" AlternatingItemStyle-Font-Name="Times New Roman" AlternatingItemStyle-BackColor="#E7EFFF" Font-Name="Times New Roman" BackColor="#E7EFFF" Headerstyle-Font-Size="12" Headerstyle-Font-Name="Arial" Headerstyle-ForeColor="blue" Headerstyle-BackColor="#BDCFE7" cellspacing="0" width="350" cellpadding="4" GridLines="Both">

‘There are two bound columns, party and partyvotes. The latter has the DataFormatString set to {0:#,###} to display the thousands separator – 9,534,459 is much easier to read than 9534459.’


<asp:BoundColumn HeaderText="Party" DataField="party" />

<asp:BoundColumn HeaderText="Votes" ItemStyle-HorizontalAlign="right" DataFormatString="{0:#,###}" DataField="partyvotes" />