'
'Guillermo Julca
'SW506- Assignment 4
'Spring 2006
'***********************************************************************************************
|
Assignment |
|
In this
Assignment, you will REDO ASSIGNMEN2 TO USE XML WEB SERVICES AND ADO.NETand
develop your own user defined control called ZipCodeLookup.
When placed on a Windows Form, it displays three labels and textboxes. The first
label/Textbox asks for input from the user for a valid US Zip Code. Upon
entering the zipcode, the control automatically fills in the other two
label/textboxes with City and State information corresponding to the zip code.
§ Prepare two projects within one blank solution
· Windows User Control: ZipCodeLookup:
· Windows Application : TestZipCodeLookup Add User Control on to Toolbox under My User Controls so you can drag it on to the form. Remember to add your name to the caption of the form.
· Web Service : ZipCodeLookupWS with 3 Public Methods
o GetCity (byval strZipCode as string) as string, returns city
o GetState (byval strZipCode as string) as string, returns state
o GetCityState (byval strZipCode as string) as string, returns comma separated list of city, state
§ How will you figure our city/state for each zip code.
· I want you to use an XML WEB SERVICE
· When a zipcode is entered, Invoke the appropriate method on the web service to obtain the result.
· Inside the Web Service, make sure you access the Access MDB file using ADO.Net to search for the City/State corresponding to the ZipCode provided. Use the following MDB file's data as your source.
· FOR THIS ASSIGNMENT, USE ALL ZIPCODES .
· FOR THIS ASSIGNMENT, your Web Service is on the same machine, however the code should work even if placed on a different, public IP addressable machine. .
§ Additional Restrictions
Here are some additional requirements, before you steal somebody else's code and use it in your assignment.
· The Zip Code Textbox will only accept NUMERIC ENTRIES, 0 to 9 alone
· The Zip Code Textbox will only accept 5 numeric entries
· The other two text boxes cannot be "edited". Their contents can be selected and copied to clipboard but not changed.
· The ZipCodeLookup control will have a custom "bitmap" associated with that is visible in the Toolbox.
|
The Deliverables |
|
Both, hardcopy and floppy disk!
'*************************************************************************************************************
Imports System.Web
Imports
System.Web.Services
Imports
System.Web.Services.Protocols
Imports
System.Data
<WebService(Namespace:="http://localhost/Service")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
_
Public Class ZipCodeLookupWS
Inherits
System.Web.Services.WebService
'This method will
pass a Zip Code as value parameter and will use the local MDB file data
'to return
corresponding city name for that specific zip code.
<WebMethod()> _
Public Function GetCity(ByVal
strZipCode As String)
As String
Dim
strCity As String
Dim
aConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\ZipCodeDB.mdb")
Dim
strSQL As String
strSQL = "select
City from Zipcodes where Zipcode = " & "'"
& strZipCode & "'"
Dim
aCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSQL, aConnection)
aConnection.Open()
Dim
aReader As OleDb.OleDbDataReader =
aCommand.ExecuteReader(CommandBehavior.CloseConnection)
strCity = ""
While
aReader.Read
strCity =
aReader.GetValue(0).ToString
End While
'aReader.Close()
aConnection.Close()
Return
strCity
End Function
'This method will
pass a Zip Code as value parameter and will use the local MDB file data
'to return
corresponding state name for that
specific zip code.
<WebMethod()> _
Public Function GetState(ByVal
strZipCode As String)
As String
Dim
strState As String
Dim
aConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\ZipCodeDB.mdb")
Dim
strSQL As String
strSQL = "select
State from Zipcodes where Zipcode = " & "'" & strZipCode & "'"
Dim
aCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSQL, aConnection)
aConnection.Open()
Dim
aReader As OleDb.OleDbDataReader =
aCommand.ExecuteReader()
strState = ""
While
aReader.Read
strState =
aReader.GetValue(0).ToString
End While
aReader.Close()
aConnection.Close()
Return
strState
End Function
'This method will
pass a Zip Code as value parameter and will use the local MDB file data
'to return
corresponding comma separated list of city,state for that specific zip code.
<WebMethod()> _
Public Function GetCityState(ByVal
strZipCode As String)
As String
Dim
strCityState As String
Dim
aConnection As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\ZipCodeDB.mdb")
Dim
strSQL As String
strSQL = "select
State,City from Zipcodes where Zipcode = " & "'" & strZipCode & "'"
Dim
aCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand(strSQL, aConnection)
aConnection.Open()
Dim
aReader As OleDb.OleDbDataReader =
aCommand.ExecuteReader()
strCityState = ""
While
aReader.Read
strCityState =
aReader.GetValue(0).ToString & ","
& aReader.GetValue(1).ToString
End While
aReader.Close()
aConnection.Close()
Return
strCityState
End Function
End Class


'
'Guillermo Julca
'SW506- Assignment 4
'Spring 2006
Public Class Form1
'This windows app
will test our "ZipCodeLookup" windows control
'Library Project.
End Class

'
'Guillermo Julca
'SW506- Assignment 4
'Spring 2006
<ToolboxBitmap(GetType(ZipCodeLookupCtrl), "ZipCodeLookupCtrl")>
_
Public Class ZipCodeLookupCtrl
Private
blnIsNumericEntry As Boolean
Private
blnIsZipCodeLenLessThan6 As Boolean
Private
blnIsZipCodeLenEqualTo5 As Boolean
Private Sub txtZipCode_KeyDown(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyEventArgs) Handles
txtZipCode.KeyDown
' Handle the
KeyDown event to determine the type of character entered into the txtZipCode
control.
' Determine
whether the keystroke is a number from the keypad Or
' Determine
whether the keystroke is a number from the top of the keyboard Or
' Determine
whether the keystroke is a backspace.
If
(e.KeyCode >= Keys.NumPad0 And e.KeyCode
<= Keys.NumPad9) _
Or
(e.KeyCode >= Keys.D0 And e.KeyCode <=
Keys.D9) _
Or
(e.KeyCode = Keys.Back) _
Then
' A
numerical keystroke was pressed.
' Set the
flag(blnIsNumericEntry) to true and evaluate in KeyPress event.
blnIsNumericEntry = True
Else
' A
non-numerical keystroke was pressed.
' Set the
flag(blnIsNumericEntry) to False and evaluate in KeyPress event.
blnIsNumericEntry = False
End If
' Handle the
KeyDown event to determine if the length of the txtZipCode control
' is not
greater than 5 digits.
If
(sender.TextLength <= 4) Then
blnIsZipCodeLenLessThan6 = True
If
(sender.TextLength = 4) _
And
(e.KeyCode <> Keys.Back) Then
blnIsZipCodeLenEqualTo5 = True
Else
blnIsZipCodeLenEqualTo5 = False
End
If
Else
If
(e.KeyCode = Keys.Back) Then
blnIsZipCodeLenLessThan6 = True
blnIsZipCodeLenEqualTo5 = False
Else
blnIsZipCodeLenLessThan6 = False
End
If
End If
End Sub
' The KeyPress
event occurs after the KeyDown event and we will use
' to prevent
non-numerical characters from entering the txtZipCode control.
Private Sub txtZipCode_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
txtZipCode.KeyPress
' Check for
the flag being set in the KeyDown event.
If
blnIsNumericEntry = False _
Or
blnIsZipCodeLenLessThan6 = False Then
' Stop
the character from being entered into the txtZipCode
' control
since it is non-numerical or it has a size greater than 5 digits.
e.Handled = True
End If
End Sub
'The KeyUp event
occurs when a key is released while the txtZipCode control has focus.
Private Sub txtZipCode_KeyUp(ByVal
sender As System.Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles
txtZipCode.KeyUp
If
(blnIsNumericEntry) _
And
(blnIsZipCodeLenLessThan6 = True) _
And
(blnIsZipCodeLenEqualTo5 = True) _
Then
'To call
subroutine that will assign the City and State value for a specific
'Zipcode
target value for the corresponding "txtCity" and "txtState"
text boxes
GetCityAndStateForZipCodeTarget()
End If
If
(blnIsZipCodeLenEqualTo5 = False) Then
'To reset
the values for the "txtCity" and "txtState" text boxes
txtCity.Text = ""
txtState.Text = ""
End If
End Sub
Private Sub ZipCodeLookupCtrl_Load(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub GetCityAndStateForZipCodeTarget()
'This
subroutine will assign the City and State value for a specific
'Zipcode
target value for the corresponding "txtCity" and "txtState"
text boxes
Dim
strZipCodeTarget As String
Dim
strCity As String
Dim
strSate As String
Dim
strCitySate As String
'Consuming
the Service
Dim ws As New
localhost.ZipCodeLookupWS
'To get
ZipCode Target
strZipCodeTarget =
Trim(txtZipCode.Text)
'To invoke
the WebMethods implemented in the web server
strCity = ws.GetCity(strZipCodeTarget)
strSate = ws.GetState(strZipCodeTarget)
strCitySate =
ws.GetCityState(strZipCodeTarget)
txtCity.Text = strCity
txtState.Text = strSate
End Sub
End Class
‘*****************************************************************************************************************
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
_
Partial Class ZipCodeLookupCtrl
Inherits
System.Windows.Forms.UserControl
'UserControl1
overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub
Dispose(ByVal disposing As Boolean)
If
disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the
Windows Form Designer
Private
components As System.ComponentModel.IContainer
'NOTE: The
following procedure is required by the Windows Form Designer
'It can be
modified using the Windows Form Designer.
'Do not modify it
using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.lblZipCode
= New System.Windows.Forms.Label
Me.lblCity
= New System.Windows.Forms.Label
Me.lblState
= New System.Windows.Forms.Label
Me.txtZipCode
= New System.Windows.Forms.TextBox
Me.txtCity
= New System.Windows.Forms.TextBox
Me.txtState
= New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'lblZipCode
'
Me.lblZipCode.AutoSize
= True
Me.lblZipCode.Location
= New System.Drawing.Point(42, 66)
Me.lblZipCode.Name
= "lblZipCode"
Me.lblZipCode.Size
= New System.Drawing.Size(50, 13)
Me.lblZipCode.TabIndex
= 0
Me.lblZipCode.Text
= "ZipCode:"
'
'lblCity
'
Me.lblCity.AutoSize
= True
Me.lblCity.Location
= New System.Drawing.Point(42, 104)
Me.lblCity.Name
= "lblCity"
Me.lblCity.Size
= New System.Drawing.Size(27, 13)
Me.lblCity.TabIndex
= 1
Me.lblCity.Text
= "City:"
'
'lblState
'
Me.lblState.AutoSize
= True
Me.lblState.Location
= New System.Drawing.Point(42, 141)
Me.lblState.Name
= "lblState"
Me.lblState.Size
= New System.Drawing.Size(35, 13)
Me.lblState.TabIndex
= 2
Me.lblState.Text
= "State:"
'
'txtZipCode
'
Me.txtZipCode.Location
= New System.Drawing.Point(98, 63)
Me.txtZipCode.Name
= "txtZipCode"
Me.txtZipCode.Size
= New System.Drawing.Size(135, 20)
Me.txtZipCode.TabIndex
= 3
'
'txtCity
'
Me.txtCity.BackColor
= System.Drawing.SystemColors.Control
Me.txtCity.Location
= New System.Drawing.Point(98, 101)
Me.txtCity.Name
= "txtCity"
Me.txtCity.ReadOnly
= True
Me.txtCity.Size
= New System.Drawing.Size(135, 20)
Me.txtCity.TabIndex
= 4
'
'txtState
'
Me.txtState.Location
= New System.Drawing.Point(98, 138)
Me.txtState.Name
= "txtState"
Me.txtState.ReadOnly
= True
Me.txtState.Size
= New System.Drawing.Size(135, 20)
Me.txtState.TabIndex
= 5
'
'ZipCodeLookupCtrl
'
Me.AutoScaleDimensions
= New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode
= System.Windows.Forms.AutoScaleMode.Font
Me.Controls.Add(Me.txtState)
Me.Controls.Add(Me.txtCity)
Me.Controls.Add(Me.txtZipCode)
Me.Controls.Add(Me.lblState)
Me.Controls.Add(Me.lblCity)
Me.Controls.Add(Me.lblZipCode)
Me.MinimumSize
= New System.Drawing.Size(250, 200)
Me.Name
= "ZipCodeLookupCtrl"
Me.Size
= New System.Drawing.Size(300, 250)
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents lblZipCode As
System.Windows.Forms.Label
Friend WithEvents lblCity As
System.Windows.Forms.Label
Friend WithEvents lblState As
System.Windows.Forms.Label
Friend WithEvents txtZipCode As
System.Windows.Forms.TextBox
Friend WithEvents txtCity As
System.Windows.Forms.TextBox
Friend WithEvents txtState As
System.Windows.Forms.TextBox
End Class