Reputation: 3521
I want to directly convert the booleans coming from my sqlserver database as bits to strings. Problem is, it says it is not valid as a boolean and i can't figure out why not since bit in sql is either 0 or 1
<asp:TemplateField HeaderText="Estado" SortExpression="EstadoInventario">
<ItemTemplate>
<asp:Label ID="lblEstadoArtigo" runat="server"
CssClass='<%# (Boolean.Parse(Eval("EstadoInventario").ToString())) ? "badge badge-success" : "badge badge-danger" %>'
Text='<%# (Boolean.Parse(Eval("EstadoInventario").ToString())) ? "Aberto" : "Fechado" %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
I just want to show the text based on it's value and change class as well
Upvotes: 0
Views: 666
Reputation: 24957
Since your Eval()
function returns either "1" or "0" string instead of true
or false
which required by bool.Parse()
method, you need to convert it into numeric representation before using Convert.ToBoolean()
, or simply use Convert.ToBoolean()
directly with Eval("EstadoInventario")
without ToString()
:
<%-- alternative 1 --%>
<asp:Label ID="lblEstadoArtigo" runat="server"
CssClass='<%# (Convert.ToBoolean(Convert.ToInt32(Eval("EstadoInventario").ToString()))) ? "badge badge-success" : "badge badge-danger" %>'
Text='<%# (Convert.ToBoolean(Convert.ToInt32(Eval("EstadoInventario").ToString()))) ? "Aberto" : "Fechado" %>'>
</asp:Label>
<%-- alternative 2 --%>
<asp:Label ID="lblEstadoArtigo" runat="server"
CssClass='<%# (Convert.ToBoolean(Eval("EstadoInventario"))) ? "badge badge-success" : "badge badge-danger" %>'
Text='<%# (Convert.ToBoolean(Eval("EstadoInventario"))) ? "Aberto" : "Fechado" %>'>
</asp:Label>
Related issue:
Convert.ToBoolean fails with "0" value
Upvotes: 1