Solved

DBNULL

Posted on 2004-09-14
13
245 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:6784
  • 6
  • 4
  • 3
13 Comments
 
LVL 4

Accepted Solution

by:
srcalc earned 100 total points
ID: 12060321
       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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12060367
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
 

Author Comment

by:6784
ID: 12060370
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
 

Author Comment

by:6784
ID: 12060382
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
 
LVL 4

Expert Comment

by:srcalc
ID: 12060395
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
 

Author Comment

by:6784
ID: 12060414
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12060441
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
 

Author Comment

by:6784
ID: 12060489
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
 
LVL 4

Expert Comment

by:srcalc
ID: 12060493
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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12060656
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
 

Author Comment

by:6784
ID: 12066699
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
 
LVL 18

Assisted Solution

by:DotNetLover_Baan
DotNetLover_Baan earned 100 total points
ID: 12067581
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
 

Author Comment

by:6784
ID: 12070721
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now