Results 1 to 3 of 3

Thread: Nulls and While dr.Read question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Nulls and While dr.Read question

    I am pulling some data to my form from a SQL Server database. I have one field that sometimes has a value and other times has a null. If I try to use the following code I get an error if the class field is null in the database. How can I adjust this code to accept null values if need be?
    VB Code:
    1. dr = cmd.ExecuteReader
    2.         If dr.HasRows Then
    3.             While dr.Read()
    4.                 txtFName.Text = dr("FirstName")
    5.                 txtLName.Text = dr("LastName")
    6.                 txtRate.Text = Format(dr("HrlyRate"), "Fixed")
    7.                 txtBaseRate.Text = Format(dr("HrlyRate"), "Fixed")
    8.                 txtPrDept.Text = dr("PRDept")
    9.                 txtCrew.Text = dr("Crew")
    10.                 txtClass.Text = dr("Class")

  2. #2
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    Just Check it for a null value

    Code:
      dr = cmd.ExecuteReader
            If dr.HasRows Then
                While dr.Read()
                    txtFName.Text = dr("FirstName")
                    txtLName.Text = dr("LastName")
                    txtRate.Text = Format(dr("HrlyRate"), "Fixed")
                    txtBaseRate.Text = Format(dr("HrlyRate"), "Fixed")
                    txtPrDept.Text = dr("PRDept")
                    txtCrew.Text = dr("Crew")
                    If Not IsDBNull(dr("Class") then              
                         txtClass.Text = dr("Class")
                    else
                         txtClass.Text=""
                    End if

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Thank you very much Hole-In-One. That worked perfectly.

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