Solved

DBNULL

Posted on 2004-09-14
13
246 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expando 4 46
VB.Net - Oracle BulkCopy from CSV Date Format 7 45
Receiving a string from a WebService Push 21 36
Get the selected ValueMember of Combobox 5 20
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

23 Experts available now in Live!

Get 1:1 Help Now