[RESOLVED] [02/03] To check Boolean null value-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] [02/03] To check Boolean null value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Resolved [RESOLVED] [02/03] To check Boolean null value

    Hi,

    In my form there are 11 checkbox item.

    I've created the following function to check for null boolean value.
    VB Code:
    1. Public Function NullValueBool(ByVal obj As Object) As Boolean
    2.         If IsDBNull(obj) Then
    3.             return False
    4.         Else
    5.             return True
    6.         End If
    7.     End Function

    But something is wrong here. It displayed as either all 11 checkbox items checked or none at all. Can someone help me?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,931

    Re: [02/03] To check Boolean null value

    What exactly are you passing to that method? The only thing that can be DBNull are database fields. A CheckBox cannot be DBNull and nor can its Checked property. A Boolean variable cannot be DBNull either. It you want a CheckBox to be able to represent a null value as well as True and False then you need to set its ThreeState property to True and then test its CheckState property. A value of Indeterminate corresponds to a null value.

    Also, I recommend not using IsDBNull in VB.NET. If you want to check whether a database field is null or not you can so either of these:
    VB Code:
    1. If TypeOf myDataRow("ColumnName") Is DBNull Then
    or:
    VB Code:
    1. If myDataRow("ColumnName") Is DBNull.Value Then
    Finally, what would be the point of calling your method anyway when you could simply use:
    VB Code:
    1. Not IsDBNull(obj)
    and get the same result.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: [02/03] To check Boolean null value

    I don't really understand how you set the ThreeState property.

    The obj passed to the function is taken from a database field.
    chkHoldMail.Checked = NullValueBool(ds.Tables("MailMLEdit").Rows(0).Item("HOLDMAIL"))

    What is the proper way to pass null value for a checked field?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: [02/03] To check Boolean null value

    Can u please use beginners english? I don't understand all the technical jargon used.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,931

    Re: [02/03] To check Boolean null value

    So what exactly are you trying to do? It looks to me like if the db field is Null then your CheckBox will not be checked and if it is not Null, i.e. it is either True or False, then the CheckBox will be checked. I'm guessing that that is not what you're trying to achieve. I would guess that you want the CheckBox checked if the field is True and not checked if the field is False, but what do you want if the field is Null?

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: [02/03] To check Boolean null value

    Yes, you are right. I want the checkbox checked if TRUE and unchecked if it is FALSE. If it is null, then it is FALSE too. How can I do that?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,931

    Re: [02/03] To check Boolean null value

    I'm not exactly sure what technical jargon you're referring to. This is computer programming so you can't get away without it being a bit technical. I don't really see how I could have pitched post #2 any lower. All the "technical" terms were the names of types, properties or methods. I have to use their proper names or how will you know what I'm talking about?

    Anyway, one simple way to achieve your aim is to check for null first and if the field is not null then check its Booelan value:
    VB Code:
    1. Dim value As Object = ds.Tables("MailMLEdit").Rows(0)("HOLDMAIL")
    2.  
    3. Me.chkHoldMail.Checked = Not TypeOf value Is DBNull AndAlso CBool(value)
    This first tests the field to see if it is Null. If it is then the entire expression will be False so the second condition is not checked and the CheckBox is not checked. If the field is not Null its value is cast as type Boolean and the Checked property of the CheckBox is set to that value.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: [02/03] To check Boolean null value

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.