Solved

DBNULL

Posted on 2004-09-14
13
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 51
Access/Visual Basic Question 3 42
Groupbox Control ? 2 27
vb.net, web page, SQL holding the selected data in an accessible variable 7 34
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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