The dropdownlist 'ddlCounties' in the code below contains the 32 counties of Ireland. When the page is loaded for the first time a dropdownlist of all 32 counties is displayed and the label 'Label1' displays the text 'Please select a County'. If you select an option the autopostback property of the dropdownlist which in this case is set to true forces a postback to the server with the dropdownlist's SelectedIndexChanged event being raised and the subroutine ddlCounties_SelectedIndexChanged run.
The value of the selected item is assigned to a string variable 'County'. (If you wanted the text property you could use 'ddlCounties.SelectedItem.Text'). The variable is then checked to see if it's empty and if it is our label 'Label1' will display the text 'Please select a county' otherwise the value of the selected county is inserted into our Microsoft Access database 'County'.
The Microsoft Access database has a table 'tblCounty', and two fields an 'ID' primary key autonumber and a text field 'County'. To insert the selection we have imported the System.Data.Oledb class to give us access to the OledbConnection and OledbCommand classes. We initialized a string variable sConnString to hold the connection string to our database, and another string variable 'sSQL' to hold our SQL statement. The ExecuteNonQuery method of our command object 'oCommand' executes the SQL statement.
The label 'Label1' will also display a message with the name of the county that has been selected and insert inserted.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.Oledb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub ddlCounties_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim County As String = ddlCounties.SelectedItem.Value
If County = "" Then
Label1.Text = "Please select a County"
Else
'Insert selection into database
Dim oConnection As New OledbConnection
Dim oCommand As OledbCommand
Dim sConnString As String
Dim sSQL As String
sConnString = "Provider=Microsoft.Jet.Oledb.4.0;" & _
"Data Source=H:\Inetpub\wwwroot\code\County.mdb"
oConnection = New OleDbConnection(sConnString)
sSQL="INSERT INTO tblCounty(County) Values (@County)"
oConnection.Open()
oCommand = New OleDbCommand(sSQL, oConnection)
oCommand.Parameters.Add(New OleDbParameter("@County", County))
oCommand.ExecuteNonQuery()
oConnection.Close()
Label1.Text = "You selected " & County & " and it has been added to the database."
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
Label1.Text = "Please select a County"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
<title>Counties of Ireland</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCounties" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCounties_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True">Select County</asp:ListItem>
<asp:ListItem Value="Antrim">Antrim</asp:ListItem>
<asp:ListItem Value="Armagh">Armagh</asp:ListItem>
<asp:ListItem Value="Carlow">Carlow</asp:ListItem>
<asp:ListItem Value="Cavan">Cavan</asp:ListItem>
<asp:ListItem Value="Clare">Clare</asp:ListItem>
<asp:ListItem Value="Cork">Cork</asp:ListItem>
<asp:ListItem Value="Derry">Derry</asp:ListItem>
<asp:ListItem Value="Donegal">Donegal</asp:ListItem>
<asp:ListItem Value="Down">Down</asp:ListItem>
<asp:ListItem Value="Dublin">Dublin</asp:ListItem>
<asp:ListItem Value="Galway">Galway</asp:ListItem>
<asp:ListItem Value="Fermanagh">Fermanagh</asp:ListItem>
<asp:ListItem Value="Kerry">Kerry</asp:ListItem>
<asp:ListItem Value="Kildare">Kildare</asp:ListItem>
<asp:ListItem Value="Kilkenny">Kilkenny</asp:ListItem>
<asp:ListItem Value="Laois">Laois</asp:ListItem>
<asp:ListItem Value="Limerick">Limerick</asp:ListItem>
<asp:ListItem Value="Leitrim">Leitrim</asp:ListItem>
<asp:ListItem Value="Longford">Longford</asp:ListItem>
<asp:ListItem Value="Louth">Louth</asp:ListItem>
<asp:ListItem Value="Meath">Meath</asp:ListItem>
<asp:ListItem Value="Mayo">Mayo</asp:ListItem>
<asp:ListItem Value="Monaghan">Monaghan</asp:ListItem>
<asp:ListItem Value="Offaly">Offaly</asp:ListItem>
<asp:ListItem Value="Roscommon">Roscommon</asp:ListItem>
<asp:ListItem Value="Sligo">Sligo</asp:ListItem>
<asp:ListItem Value="Tipperary">Tipperary</asp:ListItem>
<asp:ListItem Value="Tyrone">Tyrone</asp:ListItem>
<asp:ListItem Value="Waterford">Waterford</asp:ListItem>
<asp:ListItem Value="Wexford">Wexford</asp:ListItem>
<asp:ListItem Value="Westmeath">Wesmeath</asp:ListItem>
<asp:ListItem Value="Wicklow">Wicklow</asp:ListItem>
</asp:DropDownList><br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Get the best asp web hosting provider now and save 30%
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.