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
6784Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srcalcCommented:
       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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DotNetLover_BaanCommented:
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
0
6784Author Commented:
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

6784Author Commented:
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.






0
srcalcCommented:
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
0
6784Author Commented:
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.
0
DotNetLover_BaanCommented:
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.
0
6784Author Commented:
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
0
srcalcCommented:
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
0
DotNetLover_BaanCommented:
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
0
6784Author Commented:
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
                                                                                         
0
DotNetLover_BaanCommented:
Matter of fact, you don't need that..

       For Each li In ddlDefaultVatCode.Items
           If Not IsDbNull(oDr!Vat_Code) And li.Text.Substring(0, 3) = oDr!Vat_Code Then  
                    li.Selected = True
                    Exit For
          End If
       Next
0
6784Author Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.