Results 1 to 4 of 4

Thread: [RESOLVED] Dealing with Nulls using the Datareader method...

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    60

    Resolved [RESOLVED] Dealing with Nulls using the Datareader method...

    Hello all,

    I am after advice on the best method to deal with nulls returned from the DB when using the datareader method for gathering my data.

    Here is a sample of my code:

    'See if any data exists before continuing
    If objData.DataReader.HasRows Then
    'Read the first and only row of data
    objData.DataReader.Read()
    txtFirstName.Text = _
    objData.DataReader.Item("FirstName").ToString.ToUpper
    txtSurname.Text = _
    objData.DataReader.Item("LastName").ToString.ToUpper
    End If

    I get an error when this code is run along the lines of 'cannot cast DBNull to string'.

    Now I can sort out Nulls at the DB end, no problem, but I would prefer to do this at the client end.

    I've looked into the IsDBNull function but just cannot get that to work for some reason. Can someone shine any light on this please?

    Thank you

  2. #2
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Dealing with Nulls using the Datareader method...

    I've used it like below with no probs.

    VB Code:
    1. If IsDBNull(dr.GetValue(7)) Then                            
    2. oldtext = 0                          
    3. Else                            
    4. oldtext = CInt(dr.GetValue(7))                      
    5. End If

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    60

    Re: Dealing with Nulls using the Datareader method...

    Thanks

    I could see that working, but it would be long way around as far as I can see, I was hoping for something short and sweet. For example, in vba I could do this:

    txtSurname = Iif(Isnull(rs.firstname),"",rs.firstname) or something similar.

    Is there a similar method I could use specifically for the datareader in vb?

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2005
    Posts
    60

    Re: Dealing with Nulls using the Datareader method...

    Sorted, after the rebuild. The ToString sorts it out it seems.

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