Private Sub cboClientName_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cboClientName.LostFocus
'Check the backend database for existing clients based on name.
'If multiple phone numbers exist for the client, show a form to be able to select a phone number.
'If single phone number exists, just populate the form.
Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
Dim ClientInformationTable As String = "ClientInformation"
Dim ClientInformationTable_PKColumn As String = "ClientInfoID_PK"
Dim ClientInformationTable_PKFKColumn As String = "PhoneNumberTypeID_PKFK"
Dim ClientInformationTable_ClientNameColumn As String = "ClientName"
Dim ClientInformationTable_ClientPhoneNumberColumn As String = "ClientPhoneNumber"
Dim PhoneNumberTypeTable As String = "PhoneNumber_Type"
Dim PhoneNumberTypeTable_PKColumn As String = "PhoneNumberTypeID_PK"
Dim PhoneNumberTypeTable_PhoneNumberTypeNameColumn As String = "PhoneNumberTypeName"
Try
'Open the database connection.
SetDatabaseConnection.Open()
'Create a new DataSet to hold two DataTables.
Dim ClientInfoDS As DataSet = New DataSet()
'Set the locale of the DataSet.
ClientInfoDS.Locale = CurrentCulture
'Create two DataTables for each database table.
Dim ClientInfoNamePhoneDT As DataTable = New DataTable(ClientInformationTable)
Dim ClientPhoneNumberTypeDT As DataTable = New DataTable(PhoneNumberTypeTable)
'Set the locale of the DataTables.
ClientInfoNamePhoneDT.Locale = CurrentCulture
ClientPhoneNumberTypeDT.Locale = CurrentCulture
'Add some columns to the DataTables.
With ClientInfoNamePhoneDT.Columns
.Add(ClientInformationTable_PKColumn, GetType(Integer))
.Add(ClientInformationTable_ClientNameColumn, GetType(String))
.Add(ClientInformationTable_ClientPhoneNumberColumn, GetType(String))
.Add(ClientInformationTable_PKFKColumn, GetType(Integer))
End With
With ClientPhoneNumberTypeDT.Columns
.Add(PhoneNumberTypeTable_PKColumn, GetType(Integer))
.Add(PhoneNumberTypeTable_PhoneNumberTypeNameColumn, GetType(String))
End With
'Create two SQLCommands to get the information from the database.
Dim ClientInfoNamePhoneCommand As SqlCommand = New SqlCommand
Dim ClientPhoneNumberTypeCommand As SqlCommand = New SqlCommand
'Clear the dataset of previously obtained information.
ClientInfoDS.Clear()
'Define the information from the SQLCommands.
With ClientInfoNamePhoneCommand
.Connection = SetDatabaseConnection
.CommandType = CommandType.Text
.CommandText = "SELECT " & ClientInformationTable_PKColumn & "," _
& ClientInformationTable_PKFKColumn & "," _
& ClientInformationTable_ClientNameColumn & "," _
& ClientInformationTable_ClientPhoneNumberColumn & _
" FROM " & ClientInformationTable
.ExecuteNonQuery()
End With
With ClientPhoneNumberTypeCommand
.Connection = SetDatabaseConnection
.CommandType = CommandType.Text
.CommandText = "SELECT " & PhoneNumberTypeTable_PKColumn & "," _
& PhoneNumberTypeTable_PhoneNumberTypeNameColumn & _
" FROM " & PhoneNumberTypeTable
.ExecuteNonQuery()
End With
'Create two DataAdapters so that we can fill the DataTables with information.
Dim ClientInfoNamePhoneDA As SqlDataAdapter = New SqlDataAdapter(ClientInfoNamePhoneCommand)
Dim ClientPhoneNumberTypeDA As SqlDataAdapter = New SqlDataAdapter(ClientPhoneNumberTypeCommand)
'Fill the DataAdapters.
ClientInfoNamePhoneDA.Fill(ClientInfoNamePhoneDT)
ClientPhoneNumberTypeDA.Fill(ClientPhoneNumberTypeDT)
'Now add the DataTables to the DataSet.
With ClientInfoDS
.Tables.Add(ClientInfoNamePhoneDT)
.Tables.Add(ClientPhoneNumberTypeDT)
End With
'Create a new relationship between the two DataTables in the DataSet.
Dim ClientInfoDRe As DataRelation
Dim ParentTable, ChildTable As DataColumn
With ClientInfoDS
ParentTable = .Tables(PhoneNumberTypeTable).Columns(PhoneNumberTypeTable_PKColumn) 'PK (PhoneNumber_Type)
ChildTable = .Tables(ClientInformationTable).Columns(ClientInformationTable_PKFKColumn) 'PKFK (ClientInformation)
ClientInfoDRe = New DataRelation("Phone Numbers", ParentTable, ChildTable)
.Relations.Add(ClientInfoDRe)
End With
'Now create two DataRows for the records.
Dim ParentDR, ChildDR As DataRow
'Loop through the records.
For Each ParentDR In ClientInfoDS.Tables(PhoneNumberTypeTable).Rows
For Each ChildDR In ParentDR.GetChildRows(ClientInfoDRe)
'Check to see if there are actually records available.
If ClientInfoDS.Tables(PhoneNumberTypeTable).Rows.Count > 0 Then
'Records available.
ChildDR = ClientInfoDS.Tables(ClientInformationTable).Rows.Find(cboClientName.Text.Trim)
Select Case ClientInfoDS.Tables(ClientInformationTable).Rows.Count
Case 1 'One record only.
'Now if there is only one phone number returned, populate the other controls.
'Populate the phone number field.
mtbClientPhoneNumber.Text = ChildDR(ClientInformationTable_ClientPhoneNumberColumn).ToString
cboPhoneNumberType.Text = ParentDR(PhoneNumberTypeTable_PhoneNumberTypeNameColumn).ToString
Case Else 'Multiple records returned.
'Show a form for the end-user to choose the phone number and phone number type they want.
End Select
Else
'Nothing returned.
'Focus the phone number field.
mtbClientPhoneNumber.Focus()
End If
Next ChildDR
Next ParentDR
'Close the database connection.
SetDatabaseConnection.Close()
Catch ex As SqlException
MessageBox.Show("There was an error retrieving data from the databse." & vbNewLine & vbNewLine & _
"The reason why you got this error in the first place is:" & vbNewLine & _
"No data items in the database to pull." & vbNewLine & vbNewLine & _
"The exception error is:" & vbNewLine & _
ex.Message, _
"Database Information Retrieval Issues.", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, _
False)
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message, _
"Invalid Operation Exception.", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, _
False)
Catch ex As NullReferenceException
MessageBox.Show(ex.Message, _
"Null Reference Exception.", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, _
False)
Catch ex As ArgumentException
MessageBox.Show(ex.Message, _
"Argument Exception.", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly, _
False)
End Try
End Using
End Sub