4

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.

3 Answers 3

3

You could check for DBNull first:

If DBNull.Value.Equals(objStatus) Then
  Return False
Else
  Return objStatus
End If
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.