Link to home
Start Free TrialLog in
Avatar of 6784
6784

asked on

DBNULL

I need a way to test for Null values because my app. currently crashes on them
here is a portion of my code. I would need the test in here somewhere.

For Each li In ddlDefaultVatCode.Items
                        If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                            li.Selected = True
                            Exit For
                        End If
                    Next

thanks
ASKER CERTIFIED SOLUTION
Avatar of srcalc
srcalc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DotNetLover_Baan
DotNetLover_Baan

You can use the IsDbNull() function to test for null value..

                    For Each li In ddlDefaultVatCode.Items
                        If Not IsDbNull(li.Text) And li.Text<>"" Then      ' "" is not a NULL
                            If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                                li.Selected = True
                                Exit For
                            End If
                        Else
                            'Do what you want to do with Null values.
                        End If
                    Next
-Baan
Avatar of 6784

ASKER

I don't think that example would work. Heres more info on what I am doing.

All my dropdownlists have already been populated. Now I am getting default values for a particular country code. When the two values match (database vs dropdownlist) that becomes the selected item. Is there away to make the null value act as any unmatched item and keep looping.
Avatar of 6784

ASKER

For Each li In ddlDefaultVatCode.Items
            If li.Text = DBNull Then
                MsgBox("Null value!")
            End If
            If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                li.Selected = True
                Exit For
            End If
        Next

Srcak

I don't think that example would work. Heres more info on what I am doing.

All my dropdownlists have already been populated. Now I am getting default values for a particular country code. When the two values match (database vs dropdownlist) that becomes the selected item. Is there away to make the null value act as any unmatched item and keep looping.






I assume that you are using a combobox? If so:

        For Each li In ddlDefaultVatCode.Items
            If Not yourComboBox.Items.IndexOf(li.text) <> -1 Then ' "" is not a NULL
                If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                    li.Selected = True
                    Exit For
                End If
            Else
                'Do what you want to do with Null values.
            End If
        Next
Avatar of 6784

ASKER

When I get a null value is there a way to keep the procedure going (looping)

when you say ''Do what you want to do with Null values.
 
I want the loop to continue but I don't know how to do it.
The logic I gave you in my post, it is doing exactly what you want. We are not exiting from the FOR loop. It is just skipping the NULL values.
Avatar of 6784

ASKER

sorry for not putting more info in the first place

the null value isn't comming from the listitem its comming from the database thats being queried

 For Each li In ddlDefaultVatCode.Items
            If Not yourComboBox.Items.IndexOf(li.text) <> -1 Then ' "" is not a NULL
                If li.Text.Substring(0, 3) = oDr!Vat_Code Then  ******this is null*********
                    li.Selected = True
                    Exit For
                End If
            Else
                'Do what you want to do with Null values.
            End If
        Next
then you could just do this:

        For Each li In ddlDefaultVatCode.Items
            If Not yourComboBox.Items.IndexOf(oDr!Vat_Code) <> -1 Then
                If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                    li.Selected = True
                    Exit For
                End If
            Else
            'Do what you want to do with Null values.
            End If
        Next
lol... damn... I am such a fool.. it was understood... I don't know what I was thinking...
Ok.. try this..

         For Each li In ddlDefaultVatCode.Items
            If Not yourComboBox.Items.IndexOf(li.text) <> -1 Then ' "" is not a NULL
                If Not IsDbNull(oDr!Vat_Code) And li.Text.Substring(0, 3) = oDr!Vat_Code Then  
                    li.Selected = True
                    Exit For
                End If
            Else
                'Do what you want to do with Null values.
            End If
        Next
Avatar of 6784

ASKER

Srcalc-

your suggestion produced an error of: System.InvalidCastException: Specified cast is not valid. I am guessing its talking about the second line

        For Each li In ddlDefaultVatCode.Items
            If Not yourComboBox.Items.IndexOf(oDr!Vat_Code) <> -1 Then
                If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                    li.Selected = True
                    Exit For
                End If
            Else
            'Do what you want to do with Null values.
            End If
        Next


DotNetLover__Baan

With yours I get this error: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.ListItem'.

Its talking about this:  If Not yourComboBox.Items.IndexOf****(li.text)******* <> -1 Then ' "" is not a NULL

I appreciate the help you guys
                                                                                         
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 6784

ASKER

This is what I came up with.

 For Each li In ddlDefaultVatCode.Items
                        If (IsDBNull(oDr!Vat_Code)) = True Then
                            Exit For
                            Else
                            If li.Text.Substring(0, 3) = oDr!Vat_Code Then
                                li.Selected = True
                                Exit For
                            End If
                        End If
                    Next

I am going to split the points since you both added alot.
Thanks