Results 1 to 10 of 10

Thread: Proper Code help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Proper Code help

    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]

  2. #2
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Proper Code help

    A few things:

    1 - What is the error that you are getting?
    2 - What line is the error happening?
    3 - What are your SQLCommand.CommandText properties?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Re: Proper Code help

    1. An unhandled exception of type 'System.NullReferenceException' occurred in BiWeeklyMock.exe

    Additional information: Object reference not set to an instance of an object.
    I keep getting this error on other things throughout my project. What am I doing wrong?

    2.
    VB Code:
    1. If txtNmbNumber.Text = drCustomersUpdate("IDNumber") Then
    2.             ' txtNmbNumber.ReadOnly = True
    3.             btnFind.Enabled = False
    4.             ' Else
    5.             '  MessageBox.Show("Please enter in a valid NMB Number", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    6.             ' txtNmbNumber.Focus()
    7.             ' txtNmbNumber.SelectAll()
    8.             '  txtNmbNumber.ReadOnly = False
    9.             '  btnFind.Enabled = True
    10.         End If

    3. Huh?

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Proper Code help

    Also, the Try.. Catch.. Finally block is better to use than the On Error and labels. Also, are you using different tables and SQL Queries for each column? Why not build a join query and use one big statement?

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Re: Proper Code help

    I have had very bad luck with join statements, when using the data adapter wizard. All of my tables have an idenity key of an ID number and it hasn't liked that at all. How do I program the SQLDataAdapter without the wizard, and using a join statement?

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Proper Code help

    Do the identity keys match up throughout the tables?

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Re: Proper Code help

    Yes, all the same IDs.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Proper Code help

    Whenever you get a null reference exception it means that you have declared a variable but not created an object. This is the basis of OOP. When dealing with classes, the variable IS NOT the object. The variable contains the memory address of the object. If you declare a variable it is at first a null reference, i.e. it contains memory address zero and referes to no object. You have to create an object, usually using the "New" keyword, and assign it to the variable, which will then contain a valid memory address and refere to the object you created.

    It also sounds like you need to read up a bit on SQL. There is no "bad luck" with joins. If you write the SQL code correctly they will work, whatever method you use to create your data access objects, including the Data Adapter Configuration Wizard. Just note that the wizard cannot create the other statements for you when the query is a join because it has no idea which table to update, so you need to write the Insert, Update and Delete statements yourself in that instance.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Proper Code help

    With unique ID numbers across tables, a join statement is likely to work well. AS jmcilhinney say - you should read up on SQL a bit.

    http://www.w3schools.com/sql/sql_intro.asp

    This is what I learned it with, pretty good from the ground up.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Location
    Madison, WI
    Posts
    136

    Re: Proper Code help

    Just note that the wizard cannot create the other statements for you when the query is a join because it has no idea which table to update, so you need to write the Insert, Update and Delete statements yourself in that instance.
    Stupid question....
    where do I put the INSERT, UPDATE, and DELETE statements within the VB code?
    After I call the SQL adapter fill?
    cally

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width