Question

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.

Was it helpful?

Solution

You could check for DBNull first:

If DBNull.Value.Equals(objStatus) Then
  Return False
Else
  Return objStatus
End If

OTHER TIPS

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
<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow