Here's my code for a "find" customer button. but the only way it works is to add error handling code. I need some help with the proper way to code this.
What I need to happen is when a "wrong" ID number is entered, it shows a Message box. And if the right number is entered, then display the customers info. So far only the latter works without the error handling.
The ID number is a key column in SQL, and when a wrong number is entered it returns an error message saying the "wrong" number is a NULL value.

VB Code:
  1. Private Sub btnQVSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
  2.         If txtNmbNumber.Text = "" Or txtNmbNumber.Text = "NMB" Then
  3.             MessageBox.Show("Please enter in a NMB Number", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  4.             txtNmbNumber.Focus()
  5.             txtNmbNumber.SelectAll()
  6.             txtNmbNumber.ReadOnly = False
  7.             btnFind.Enabled = True
  8.             Exit Sub
  9.         End If
  10.         'popluate all fields that relate to id number entered
  11.         SqlDataAdapter1.Fill(DsCustomers1, "Customer")
  12.         SqlDataAdapter2.Fill(DsDrafts1, "Draft")
  13.         SqlDataAdapter3.Fill(DsPayables1, "Payables")
  14.         SqlDataAdapter4.Fill(DsAchNewbie1, "Ach")
  15.         SqlDataAdapter5.Fill(DsPayableTrans1, "Payables_Transaction")
  16.  
  17.         Dim dtCustomersUpdate As DataTable = DsCustomers1.Tables.Item("Customer")
  18.         Dim dtDraftsUpdate As DataTable = DsDrafts1.Tables.Item("Draft")
  19.         Dim dtPayablesUpdate As DataTable = DsPayables1.Tables.Item("Payables")
  20.         Dim dtAchUpdate As DataTable = DsAchNewbie1.Tables.Item("Ach")
  21.         Dim dtPayablesTransUpdate As DataTable = DsPayableTrans1.Tables.Item("Payables_Transaction")
  22.  
  23.         drCustomersUpdate = dtCustomersUpdate.Rows.Find(txtNmbNumber.Text)
  24.         drDraftsUpdate = dtDraftsUpdate.Rows.Find(txtNmbNumber.Text)
  25.         drPayablesUpdate = dtPayablesUpdate.Rows.Find(txtNmbNumber.Text)
  26.         drPayablesTransUpdate = dtPayablesTransUpdate.Rows.Find(txtNmbNumber.Text)
  27.         drAchUpdate = dtAchUpdate.Rows.Find(txtNmbNumber.Text)
  28.         Dim Msg As String
  29.         'If an error occurs, construct an error message.
  30.         On Error Resume Next
  31.         ' MessageBox.Show("Please enter in a valid NMB Number", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ' Defer error handling.
  32.         Err.Clear()
  33.         'If the number is correct
  34.         'Customers Info Tab
  35.         If txtNmbNumber.Text = drCustomersUpdate("IDNumber") Then
  36.             ' txtNmbNumber.ReadOnly = True
  37.             btnFind.Enabled = False
  38.             ' Else
  39.             '  MessageBox.Show("Please enter in a valid NMB Number", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  40.             ' txtNmbNumber.Focus()
  41.             ' txtNmbNumber.SelectAll()
  42.             '  txtNmbNumber.ReadOnly = False
  43.             '  btnFind.Enabled = True
  44.         End If
  45.         If drCustomersUpdate("Agent") Is DBNull.Value Then
  46.             txtAgent.Text = ""
  47.         Else : txtAgent.Text = drCustomersUpdate("Agent")
  48.         End If
  49.         txtLastName.Text = drCustomersUpdate("LastName")
  50.         txtFirstName.Text = drCustomersUpdate("FirstName")
  51.         txtSsn1.Text = drCustomersUpdate("SSN1")
  52.         'If field is null... NOTE: fields that are required should not be null so they won't need this code
  53.         If drCustomersUpdate("LastName2") Is DBNull.Value Then
  54.             txtLastNameSpouse.Text = ""
  55.         Else : txtLastNameSpouse.Text = drCustomersUpdate("LastName2")
  56.         End If
  57.         If drCustomersUpdate("FirstName2") Is DBNull.Value Then
  58.             txtFirstNameSpouse.Text = ""
  59.         Else : txtFirstNameSpouse.Text = drCustomersUpdate("FirstName2")
  60.         End If
  61.         If drCustomersUpdate("SSN2") Is DBNull.Value Then
  62.             txt2SSN.Text = ""
  63.         Else : txt2SSN.Text = drCustomersUpdate("SSN2")
  64.         End If
  65.         If drCustomersUpdate("Address") Is DBNull.Value Then
  66.             txtAddress.Text = ""
  67.         Else : txtAddress.Text = drCustomersUpdate("Address")
  68.         End If
  69.         If drCustomersUpdate("City") Is DBNull.Value Then
  70.             txtCity.Text = ""
  71.         Else : txtCity.Text = drCustomersUpdate("City")
  72.         End If
  73.         If drCustomersUpdate("State") Is DBNull.Value Then
  74.             txtState.Text = ""
  75.         Else : txtState.Text = drCustomersUpdate("State")
  76.         End If
  77.         If drCustomersUpdate("ZipCode") Is DBNull.Value Then
  78.             txtZipCode.Text = ""
  79.         Else : txtZipCode.Text = drCustomersUpdate("ZipCode")
  80.         End If
  81.         If drCustomersUpdate("Daytime") Is DBNull.Value Then
  82.             txtDaytime.Text = ""
  83.         Else : txtDaytime.Text = drCustomersUpdate("Daytime")
  84.         End If
  85.         If drCustomersUpdate("Evening") Is DBNull.Value Then
  86.             txtEvening.Text = ""
  87.         Else : txtEvening.Text = drCustomersUpdate("Evening")
  88.         End If
  89.         If drCustomersUpdate("Notes") Is DBNull.Value Then
  90.             txtNotes.Text = ""
  91.         Else : txtNotes.Text = drCustomersUpdate("Notes")
  92.         End If
  93.         'Drafts Info Tab
  94.         txtNmbNumberDraft.Text = txtNmbNumber.Text
  95.         drDraftsUpdate("IDNumber") = txtNmbNumberDraft.Text
  96.         txtFinancialInt.Text = drDraftsUpdate("Bank")
  97.         If drDraftsUpdate("BankPhone") Is DBNull.Value Then
  98.             txtDraftPhone.Text = ""
  99.         Else : txtDraftPhone.Text = drDraftsUpdate("BankPhone")
  100.         End If
  101.         If drDraftsUpdate("RoutingNumber") Is DBNull.Value Then
  102.             txtDraftRoute.Text = ""
  103.         Else : txtDraftRoute.Text = drDraftsUpdate("RoutingNumber")
  104.         End If
  105.         txtDraftSaves.Text = drDraftsUpdate("SavingsORChecking")
  106.         'Payables Tab
  107.         '1
  108.         txtNmbNumberPayables.Text = txtNmbNumber.Text
  109.         drPayablesUpdate("IDNumber") = txtNmbNumberPayables.Text
  110.         drPayablesTransUpdate("IDNumber") = txtNmbNumberPayables.Text
  111.         txtPayable.Text = drPayablesUpdate("PayableCompany")
  112.         txtNoPayables.Text = drPayablesUpdate("NumberPayables")
  113.         txtName.Text = drPayablesUpdate("NameOnAccount")
  114.         If drPayablesUpdate("PayablesPhone") Is DBNull.Value Then
  115.             txtPayablePhone.Text = ""
  116.         Else : txtPayablePhone.Text = drPayablesUpdate("PayablesPhone")
  117.         End If
  118.         If drPayablesUpdate("PayableAddress") Is DBNull.Value Then
  119.             txtPayableAddress.Text = ""
  120.         Else : txtPayableAddress.Text = drPayablesUpdate("PayableAddress")
  121.         End If
  122.         If drPayablesTransUpdate("Starting2") Is DBNull.Value Then
  123.             txtStartDateB.Text = ""
  124.         Else : txtStartDateB.Text = drPayablesTransUpdate("Starting2")
  125.         End If
  126.         If drPayablesTransUpdate("Ending2") Is DBNull.Value Then
  127.             txtEndDateB.Text = ""
  128.         Else : txtEndDateB.Text = drPayablesTransUpdate("Ending2")
  129.         End If
  130.         If drPayablesTransUpdate("Amount3") Is DBNull.Value Then
  131.             txtPaymentC.Text = ""
  132.         Else : txtPaymentC.Text = drPayablesTransUpdate("Amount3")
  133.         End If
  134.         If drPayablesTransUpdate("Period3") Is DBNull.Value Then
  135.             txtPeriodC.Text = ""
  136.         Else : txtPeriodC.Text = drPayablesTransUpdate("Period3")
  137.         End If
  138.         If drPayablesTransUpdate("Starting3") Is DBNull.Value Then
  139.             txtStartDateC.Text = ""
  140.         Else : txtStartDateC.Text = drPayablesTransUpdate("Starting3")
  141.         End If
  142.         If drPayablesTransUpdate("Ending3") Is DBNull.Value Then
  143.             txtEndDateC.Text = ""
  144.         Else : txtEndDateC.Text = drPayablesTransUpdate("Ending3")
  145.         End If
  146.         'Ach Tab
  147.         txtNmbNumberAch.Text = txtNmbNumber.Text
  148.         drAchUpdate("IDNumber") = txtNmbNumberAch.Text
  149.         If drAchUpdate("Amount") Is DBNull.Value Then
  150.             txtAchAmount.Text = ""
  151.         Else : txtAchAmount.Text = drAchUpdate("Amount")
  152.         End If
  153.         If drAchUpdate("FeeCode") Is DBNull.Value Then
  154.             txtFeeCode.Text = ""
  155.         Else : txtFeeCode.Text = drAchUpdate("FeeCode")
  156.         End If
  157.         If drAchUpdate("Starting") Is DBNull.Value Then
  158.             txtStarting.Text = ""
  159.         Else : txtStarting.Text = drAchUpdate("Starting")
  160.         End If
  161.         If drAchUpdate("Ending") Is DBNull.Value Then
  162.             txtEnding.Text = ""
  163.         Else : txtEnding.Text = drAchUpdate("Ending")
  164.         End If
  165.         If drAchUpdate("Amount1") Is DBNull.Value Then
  166.             txtAchAmount2.Text = ""
  167.         Else : txtAchAmount2.Text = drAchUpdate("Amount1")
  168.         End If
  169.         If drAchUpdate("FeeCode1") Is DBNull.Value Then
  170.             txtFeeCode2.Text = ""
  171.         Else : txtFeeCode2.Text = drAchUpdate("FeeCode1")
  172.         End If
  173.         If drAchUpdate("Starting1") Is DBNull.Value Then
  174.             txtStarting2.Text = ""
  175.         Else : txtStarting2.Text = drAchUpdate("Starting1")
  176.         End If
  177.         If drAchUpdate("Ending1") Is DBNull.Value Then
  178.             txtEnding2.Text = ""
  179.         Else : txtEnding2.Text = drAchUpdate("Ending1")
  180.         End If
  181.         If drAchUpdate("Amount2") Is DBNull.Value Then
  182.             txtAchAmount3.Text = ""
  183.         Else : txtAchAmount3.Text = drAchUpdate("Amount2")
  184.         End If
  185.         If drAchUpdate("FeeCode2") Is DBNull.Value Then
  186.             txtFeeCode3.Text = ""
  187.         Else : txtFeeCode3.Text = drAchUpdate("FeeCode2")
  188.         End If
  189.         If drAchUpdate("Starting2") Is DBNull.Value Then
  190.             txtStarting3.Text = ""
  191.         Else : txtStarting3.Text = drAchUpdate("Starting2")
  192.         End If
  193.         If drAchUpdate("Ending2") Is DBNull.Value Then
  194.             txtEnding3.Text = ""
  195.         Else : txtEnding3.Text = drAchUpdate("Ending2")
  196.         End If
  197.     End Sub
[/QUOTE]