[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
Re: Dealing with Nulls using the Datareader method...
I've used it like below with no probs.
VB Code:
If IsDBNull(dr.GetValue(7)) Then
oldtext = 0
Else
oldtext = CInt(dr.GetValue(7))
End If
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?
Re: Dealing with Nulls using the Datareader method...
Sorted, after the rebuild. The ToString sorts it out it seems.