views:

265

answers:

3

I am populating the customer object as shown below. How do I write condtions as shown below in

for eg. If isdbNull(.age) then .age=10 else .age=dr("age") end if

Public Shared Function Retrieve() As List(Of Customer)

Dim dt As DataTable = Dac.ExecuteDataTable( _ 
                "CustomerRetrieveAll", nothing)

Dim customerList As New List(Of Customer)

For Each dr As DataRow In dt.Rows 
   customerList.Add(New Customer With _ 
                {.CustomerId = CType(dr("CustomerID"), Integer), _ 
                 .LastName = dr("LastName").ToString, _
                 .age = dr("age").ToString, _ 
                 .FirstName = dr("FirstName").ToString})  
Next

Return customerList

End Function

A: 

Did you try this?:

.Age = IIf(dr("age") is DbNull, 10, dr("age"))

Hope it helps.

Sebastian
A: 

What value the age can have if it contains null?
I suggest using Nullable(Of Integer) for Age.

Also, compare it against DBNull.Value (which is used for comparison in case the underlying field contains null value).

shahkalpesh
A: 

Within the Microsoft.VisualBasic namespace there is a function called IIF. It works by evaluating an expression and returning one value if the expression is true and another if false.

So

.age = IIF(dr("age") = DBNull.Value, 10, dr("age").ToString())
rism