Return boolean from DataBinder.Eval( whenever it evalutes to DBNULL

Within an ascx file, the following code is used to determine whether a checkbox should be enabled
Enabled='<%# IIf(DataBinder.Eval(Container, "DataItem.IsCurrent")=True,True,False)%>'

It works find for editing existing data but errors with Operator '=' is not defined for type 'DBNull' and type 'Boolean'.
How can I check for DBNull and, if true, set Enabled to False?

Thank you.
<asp:CheckBox ID="cbIsCurrent" runat="server" Enabled='<%# IIf(DataBinder.Eval(Container, "DataItem.IsCurrent")=True,True,False)%>'
                   Checked='<%# DataBinder.Eval( Container, "DataItem.IsCurrent") %>' />

Open in new window

LVL 1
RichardKlineAsked:
Who is Participating?
 
nmarunConnect With a Mentor Commented:

<asp:CheckBox ID="cbIsCurrent" runat="server" Enabled='<%# GetIsCurrent(DataBinder.Eval(Container, "DataItem.IsCurrent").ToString)%>'
                   Checked='<%# DataBinder.Eval( Container, "DataItem.IsCurrent") %>' />
 
 
public bool GetIsCurrent(isItemCurrent as string)
{
    Dim blnIsCurrent as bool = False
    if (IsDbNull(isItemCurrent)) Then
        blnIsCurrent = False
    else
       if (isItemCurrent = "True") Then
          blnIsCurrent = True
       else
          blnIsCurrent = False
       end if
    end if
    return blnIsCurrent
}

Open in new window

0
 
isaackhaziCommented:
If ('value' Is Nothing OrElse IsDBNull('value')) Then
0
 
isaackhaziCommented:
If  IsDbNull('variable') = False
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
RichardKlineAuthor Commented:
nmarun,
Where should the GetIsCurrent function be placed?  
0
 
nmarunCommented:
In your code-behind file.
0
 
RichardKlineAuthor Commented:
Thanks!
I came up with this variant: as "enabled" really needs to be the opposite of "checked".   Also to treat DataBinder.Eval(Container, "DataItem.IsCurrent") as an object rather than a string.
ASCX:
            <asp:CheckBox ID="cbIsCurrent" runat="server"
            Enabled='<%# GetIsCurrent(DataBinder.Eval(Container, "DataItem.IsCurrent"), True)%>'
            Checked='<%# GetIsCurrent(DataBinder.Eval(Container, "DataItem.IsCurrent"), False)%>'
            />
Code Behind:
    Public Function GetIsCurrent(ByVal isItemCurrent As Object, ByVal ReverseIt As Boolean) As String
        Dim strIsCurrent As String = "False"
        If Not (IsDBNull(isItemCurrent)) Then
            If isItemCurrent Then
                strIsCurrent = "True"
            End If
        End If
        If ReverseIt Then
            If strIsCurrent.ToLower.StartsWith("f") Then
                strIsCurrent = "True"
            Else
                strIsCurrent = "False"
            End If
        End If
        Return strIsCurrent
     End Function
0
 
RichardKlineAuthor Commented:
Please see below for the final code version.
Thanks!.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.