-
Jun 16th, 2006, 01:32 AM
#1
Thread Starter
Lively Member
[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:
Public Function NullValueBool(ByVal obj As Object) As Boolean
If IsDBNull(obj) Then
return False
Else
return True
End If
End Function
But something is wrong here. It displayed as either all 11 checkbox items checked or none at all. Can someone help me?
-
Jun 16th, 2006, 01:45 AM
#2
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:
If TypeOf myDataRow("ColumnName") Is DBNull Then
or:
VB Code:
If myDataRow("ColumnName") Is DBNull.Value Then
Finally, what would be the point of calling your method anyway when you could simply use:and get the same result.
-
Jun 16th, 2006, 01:58 AM
#3
Thread Starter
Lively Member
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?
-
Jun 16th, 2006, 02:08 AM
#4
Thread Starter
Lively Member
Re: [02/03] To check Boolean null value
Can u please use beginners english? I don't understand all the technical jargon used.
-
Jun 16th, 2006, 02:09 AM
#5
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?
-
Jun 16th, 2006, 02:28 AM
#6
Thread Starter
Lively Member
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?
-
Jun 16th, 2006, 02:43 AM
#7
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:
Dim value As Object = ds.Tables("MailMLEdit").Rows(0)("HOLDMAIL")
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.
-
Jun 16th, 2006, 03:51 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|