tags:

views:

545

answers:

3

hi guys, i have a checkbox in gridview.

 <asp:CheckBox  ID="chkStatus"
runat="server"
 Checked='<%#GetStatus(Eval("VaccinationCompletedStatus"))
 %>'/>

The function GetStatus is as follows

  Public Function GetStatus(ByVal objStatus As Object) As Boolean
        If objStatus = True Then
            Return True
        ElseIf objStatus = False Then
            Return False
        Else
            Return False
        End If

    End Function

But if Status is coming as null from database then one error is coming as System.DBNull cannot be cast to object.If null is coming from database i want to get checkbox checked false.

+2  A: 

You could check for DBNull first:

If DBNull.Value.Equals(objStatus) Then
  Return False
Else
  Return objStatus
End If
Zaagmans
+2  A: 

Use the TypeOf function to check if the input type if a boolean. If it isn't just return false.

If TypeOf objStatus Is Boolean Then
  Your code here
Else
  Return False
End If
Rune Grimstad
A: 
<asp:TemplateField HeaderText="foo" >
    <ItemTemplate>    
        <asp:CheckBox  runat="server"
                    Checked='<%# Eval("foo").GetHashCode() == 1 %>'
                    Enabled="false"
                    Visible='<%# Eval("foo").GetType() == typeof(Boolean) %>' />
    </ItemTemplate>
</asp:TemplateField>
Rd_Madnezz