Question

I have a SQL stored proc where I am creating a column ("Certified") dynamically based on two other columns. The value from this column is a '0' or '1'. The SQL stored proc query is:

, CASE WHEN 
    (StartMiles < EndMiles) 
    AND (StartTime < EndTime) 
    AND (bcd.Status != 'C')
     THEN '1' ELSE '0' END
     AS Certified

On the front end in my aspx page, I have a telerik radgrid that will display a checkbox (enabled if value is 1, disabled if value is 0). The aspx code is:

<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" 
        OnCheckedChanged="CheckBox2_CheckedChanged" 
        Enabled='<%# !bool.Parse(Eval("Certified").ToString()) %>' />
    </ItemTemplate>
</telerik:GridTemplateColumn>  

I am getting an error on the aspx page String was not recognized as a valid Boolean
To resolve the error, how can I set a datatype in the stored proc?

Was it helpful?

Solution

<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" 
            OnCheckedChanged="CheckBox2_CheckedChanged" 
            Enabled='<%# !Convert.ToBoolean(Convert.ToInt32(Eval("Certified").ToString())) %>' />
    </ItemTemplate>
</telerik:GridTemplateColumn>

OTHER TIPS

I would recommend using a code-behind method to do this instead of putting logic into the markup via embedded code blocks, like this:

Markup:

<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" 
                            Visible="true">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" 
                    OnCheckedChanged="CheckBox2_CheckedChanged" 
                    Enabled='<%# IsCertified(Eval("Certified").ToString()) %>' />
    </ItemTemplate>
</telerik:GridTemplateColumn> 

Code-behind:

protected bool IsCertified(string certifiedValue)
{
    return !bool.Parse(certifiedValue);
}

Note: To be safer, I would recommend using the Boolean.TryParse() method instead of just the Parse(), as that will eliminate the chance of a string that cannot be parsed into a bool from throwing an exception. Read Boolean.TryParse Method documentation for more information.

This provides two advantages, in my opinion, over the OP code:

  1. Simplified markup, because you do not have conditional logic in the markup, but now just a call to the method.
  2. You can leverage the power of Visual Studio's IntelliSense, compiler to catch syntax errors at compile-time instead of run-time and the debugger itself.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top