deleteaByelection.aspxReturn to the Documentation page
  

functionsPage_Load
bindData
onBtnDeleteClicked
linkToByelectionsMenu
Page_Change

<%@ 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.’

bindData()

‘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 Access2000 which will hold the SQL statement SELECT … from byelections. The table byelections contains all the data from this parliament’s byelections and there were only 6 in total from 2001 - 2005.’

Dim Access2000 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 Access2000 and MyConn.’

Dim Cmd as New ODBCDataAdapter(Access2000,MyConn)

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

Cmd.Fill(ds,"mps")

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

gridMP.Datasource=ds.Tables("mps").DefaultView

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

gridMP.DataBind()

‘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.’

conMP.Open()

‘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. "

else

‘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.’

rdrMP.Close()

conMP.Close()

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

bindData()

‘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.’

Response.Redirect("byelections.aspx")

‘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 were only six byelections in the whole of the Parliament no page numbers are shown at the bottom of the datagrid web control. 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

bindData

</script>

<html>

<head>

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

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

</head>

<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 [ballotbox.jpg]. 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/ballotbox.jpg" title="Cast your vote wisely!"></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>

</table><P>

‘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>

<center>

‘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"

cellpadding="4"

width="600"

Headerstyle-BackColor="#BDCFE7"

Headerstyle-ForeColor="blue"

Headerstyle-Font-Name="Arial"

Headerstyle-Font-Size="12"

BackColor="#E7EFFF"

Font-Name="Arial"

Font-Size="10"

AlternatingItemStyle-BackColor="#E7EFFF"

AlternatingItemStyle-Font-Name="Arial"

AlternatingItemStyle-Font-Size="10"

BorderColor="Black"

AllowPaging = "false"

PageSize = "20"

PagerStyle-Mode = "NumericPages"

PagerStyle-HorizontalAlign="Center"

PagerStyle-PageButtonCount = "10"

OnPageIndexChanged = "Page_Change"

AutoGenerateColumns="False">

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

<columns>

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

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

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

</columns>

</asp:DataGrid>

</center>

<p>

</form>

</body>

</html>

***********************************************