KellyMarchewa KellyMarchewa - 1 year ago 141
C# Question

Tenary Operators and Eval in C# .Net - Cannot convert string to bool

I have a project that has data that may or may not have files associated with it. If the record does not have a file associated with it, I do not want to display the link/label. I thought this would be fairly simple to implement with the tenary operator, but I receive the following:

: CS0030: Cannot convert type 'string' to 'bool'

resulting from the line containing this statement

<asp:Label Visible='<%# Eval("supportingDocuments") == null ? "False" : "True" %>' id="supportingFileLink" runat="server" Text='Other' />

To me, this implies that it is seeing
Eval("supportingDocuments") == null
as a string and not a conditional that can be evaluated as a boolean. I have tried adding parentheses around the condition, but this did not change anything.

Thanks much for any guidance.

EDIT: This is an a databound GridView control; sorry for not mentioning that earlier.

Answer Source

It must not be liking the double quotes around "True" and "False".

You could try:

Visible='<%#  Eval("supportingDocuments") == null ? false : true %>'

Or better still, you can probably simplify to:

Visible='<%#  Eval("supportingDocuments") != null %>'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download