deleteaByelection.aspxReturn to the Documentation page


<%@ Page Language="vb" %>

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

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

<script runat="server">

‘When the page first loads run the bindData function below.’


‘The bindData function displays the byelections on the screen – only the constituency, first and winningparty fields are shown. The user then enters the name of the constituency (s)he wishes to delete in the text box [txtConstituency1] and then clicks the ‘Delete Constituency’ button to complete the process.’

‘Declare the variable MySQL which will hold the SQL statement SELECT … from byelections. The table byelections will contain all the data from this parliament’s byelections. To date (31/03/06) only three byelections have taken place – so there are only three rows within this table.’

Dim MySQL as string = "Select constituency, first, winningparty from byelections"

‘Set up the ODBC Connection object using the strConn parameter, the value of which is fetched from the web.config file.’

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

‘Set up a dataset ready to take the data before being pushed in to the datagrid web control [gridMP] on the user’s web page.’

Dim ds as DataSet=New DataSet()

‘Declare a variable [Cmd] of type ODBCDataAdapter with the parameters MySQL and MyConn.’

Dim Cmd as New ODBCDataAdapter(MySQL,MyConn)

‘Fill the dataset – all the relevant data is now within a table called ‘mps’.’


‘Set the Datasource property of the datagrid [gridMP] to the ‘mps’ table.’


‘Bind the data to the grid – it now displays on the user’s screen.’


‘The onBtnDeleteClicked function deletes the byelection record from the byelections table.’

‘Declare two variables – one of type ODBCCommand [cmdMP] and one of type ODBCDataReader [rdrMP].’

Dim cmdMP as ODBCCommand

Dim rdrMP as ODBCDataReader

‘Declare variable conMP of type ODBCConnection using the parameter strConn which is fetched from the web.config file.’

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

‘Open the connection to the database.’


‘Define a variable [cmdMP] of type ODBCCommand which will take the SQL statement DELETE FROM … WHERE … and issue it against the database. Whatever the user types into the textbox [txtConstituency1] will go into the WHERE… part of the statement below. The final parameter, conMP, is the connection object.’

cmdMP = New ODBCCommand ("DELETE FROM byelections WHERE constituency = ('" & txtConstituency1.Text & "')", conMP)

‘The ExecuteReader method of the ODBCCommand object executes the DELETE… statement and returns the relevant data through the ODBCDataReader object.’

rdrMP = cmdMP.ExecuteReader

‘However, if the textbox [txtConstituency1] is NOT blank AND the RecordsAffected property of the ODBCDataReader object is greater than 0, i.e. a record was actually deleted from the byelections table….’

if txtConstituency1.text <> "" and rdrMP.RecordsAffected > 0 then

‘Display a suitable message in the asp label [lblStatus] confirming that the constituency record had been deleted.’

lblStatus.Text = txtConstituency1.text & "'s record has been successfully deleted. "


‘Otherwise ask the user to enter the name of a constituency.’

lblStatus.Text = "Please enter the name of the Constituency you wish to delete."

end if

‘Reset the txtConstituency1 textbox to blank.’

txtConstituency1.Text = ""

‘Close the ODBCDataReader object and the connection object [conMP] as usual.’



‘Call the bindData function which will display all the byelections MINUS the one just deleted.’


‘The linkToByelectionsMenu function is attached to an ASP linkbutton control which displays on the top right-hand side of every page. When clicked it redirects the user back to the byelections menu.’


‘The Page_Change function deals with changing the page depending on the page number selected by the user. Each page is split into 20 records by the datagrid web control [gridMP] using the property - PageSize = "20". Since there are only two records in the byelections table at the moment no page numbers are currently displayed at the bottom of the data. This function is only going to be needed if there are more than 20 byelections and that isn’t likely to happen for most parliaments.’

gridMP.CurrentPageIndex = e.NewPageIndex





<title>Delete a By-election from the Database</title>

<link href=".\otherfiles\election2005.css" type="text/css" rel="stylesheet">


<body bgcolor="white">

‘The first part of the web form puts an ASP label into the top left-hand corner of the user’s screen together with a graphic [cross.gif]. The top right-hand corner contains an ASP linkbutton control that redirects the user back to the Byelections menu when clicked.’

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

<table width="100%">

<tr><td width="85px" rowspan="2"><img src="./images/cross.gif" title="Make voting compulsory!"></td>

<td><asp:Label id="Label1" runat="server" ><u>Delete a By-election</u></asp:Label></td>

<td align="right"><asp:LinkButton id="LinkToEdit" onclick="linkToByelectionsMenu" runat="server" Text="Return to the By-elections menu"></asp:LinkButton></td></tr>

<tr><td colspan="2">&nbsp;</td></tr>


‘The second part of the form puts an ASP label [Label2], a textbox [txtConstituency1] to receive user input and a button [btnDelete] which the user will click to run the OnBtnDeleteClicked function, already mentioned above.’

<asp:Label id="Label2" runat="server" >Enter the name of the Constituency:</asp:Label> <asp:textbox id="txtConstituency1" runat="server" ></asp:textbox> <asp:button id="btnDelete" onclick="onBtnDeleteClicked" runat="server" forecolor="White" backcolor="Navy" text="Delete Constituency"></asp:button><p>

<asp:Label id="lblStatus" runat="server" forecolor="red"></asp:Label><p>


‘The third part displays the byelection data inside an ASP datagrid [gridMP]. Users can check out the available data and delete the necessary constituency from the byelections table as instructed on the form.’

<asp:DataGrid runat="server" id="gridMP" GridLines="Both"














AllowPaging = "false"

PageSize = "20"

PagerStyle-Mode = "NumericPages"


PagerStyle-PageButtonCount = "10"

OnPageIndexChanged = "Page_Change"


‘Only three fields are to be displayed in bound columns – constituency, first and winningparty.’


<asp:BoundColumn HeaderText="Constituency" DataField="constituency" />

<asp:BoundColumn HeaderText="MP" DataField="first" />

<asp:BoundColumn HeaderText="Winning party" DataField="winningparty" />