problem displaying bool value in formtemplate type edit form insert mode

27 posts, 1 answers
  1. sf
    sf avatar
    103 posts
    Member since:
    Apr 2009

    Posted 29 Jun 2009 Link to this post

    My Column:
    <telerik:GridCheckBoxColumn DataField="IsActive" DataType="System.Boolean" ItemStyle-Width="20px" 
                            HeaderText="IsActive" SortExpression="IsActive" UniqueName="IsActive">
     </telerik:GridCheckBoxColumn>  
    My EditForm template:
    <EditFormSettings EditFormType="Template">
                        <FormTemplate>
    ......
    Active:
    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind( "IsActive") %>' TabIndex="4">
    </asp:CheckBox>         
                                             
    I was able to update any records in the grid. However when I clicked on Add New Record, I received an error "specified cast is not valid" on the boolean field "IsActive". Please help, thanks in advance
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Jun 2009 Link to this post

    Hi,

    The reason for this error is that your grid instance can not bind a value for the newly inserted item through the Eval()/Bind() syntax. A solution would be to preset the default value of the control(s) when binding to a grid item on the RadGrid.InitInsertCommandName command.

    CS:
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        if ((e.CommandName == RadGrid.InitInsertCommandName)) 
              { 
            e.Canceled = true
            //Prepare an IDictionary with the predefined values 
            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary(); 
     
            //set initial checked state for the checkbox on init insert 
            newValues("Bool") = false
     
            //Insert the item and rebind 
            e.Item.OwnerTableView.InsertItem(newValues); 
        } 
     


    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. sf
    sf avatar
    103 posts
    Member since:
    Apr 2009

    Posted 30 Jun 2009 Link to this post

    Thanks Princy, it didnt solve my problem but..

    I tried your code (see below), but still got the same error "specificed cast is not valid":

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        if ((e.CommandName == RadGrid.InitInsertCommandName)) 
              { 
            e.Canceled = true
            //Prepare an IDictionary with the predefined values 
            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary(); 
     
            //set initial checked state for the checkbox on init insert 
            newValues["CheckBox1"] = false
     
            //Insert the item and rebind 
            e.Item.OwnerTableView.InsertItem(newValues); 
        } 

    Then i tried the idea from this thread (http://www.telerik.com/community/forums/aspnet-ajax/grid/checkbox-in-editform.aspx), still no luck, instead I got different error "The name 'Bind' does not exist in the current context"
    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? "false" : Bind( "IsActive") %>'>
                                                    </asp:CheckBox>

    is there a demo or tutorial for successful implementation of CheckBox with boolean values in a formtemplate editform?

  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 01 Jul 2009 Link to this post

    Hello,

    Try replacing newValues["CheckBox1"] = false; with the following line of code and see if it helps:

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        if ((e.CommandName == RadGrid.InitInsertCommandName))  
              {  
            e.Canceled = true;  
            //Prepare an IDictionary with the predefined values  
            System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();  
      
            //set initial checked state for the checkbox on init insert  
            newValues["IsActive"] = false;  
      
            //Insert the item and rebind  
            e.Item.OwnerTableView.InsertItem(newValues);  
        }  
    }  

    You can refer to the following document as well to learn more:
    Known reasons for error messages

    Thanks
    Princy.
  6. sf
    sf avatar
    103 posts
    Member since:
    Apr 2009

    Posted 01 Jul 2009 Link to this post

    thanks Princy for your help, problem solved! 
  7. esaracing
    esaracing avatar
    1 posts
    Member since:
    Aug 2009

    Posted 11 Aug 2009 Link to this post

    Hi,
    i'm still having the error "Specified cast is not valid." even after the code you posted on jul 1.... it happens if i click "add new" and then (without actually inserting a new item, just leaving the insert form "expanded") try to "edit" an item

    error msg shown below:

    Specified cast is not valid.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Specified cast is not valid.
    Source Error:
    Line 92:         <EditItemTemplate>
    Line 93: <asp:CheckBox ID="chkbox1" Checked='<%# Bind("hasValue") %>' 
    Line 94: runat="server" AutoPostBack="True" Line 95: oncheckedchanged="chkbox1_CheckedChanged" />


    thnx in advance,



  8. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 12 Aug 2009 Link to this post

    Hello esaracing,

    You get this due to the fact that hasValue is null and Bind() expression will try to assign this to Checked property. You need to have default value (true/false) for hasValue!

    All the best,
    Vlad
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Jacques
    Jacques avatar
    135 posts
    Member since:
    Jun 2007

    Posted 28 Aug 2009 Link to this post

    Hello there guys,

    I'm having the same problem as posted in this thread and I've applied the code change recommended by Princy, which worked.

    I'm using Ajax with the Grid and when I clicked new record in the beginning I had the specified cast invalid problem, but the new code fixed that. The problem I have now is that in the UserControl popup that is controlled by the grid I enter values in the fields and click insert, the save occurrs correctly, but when I can Grid.DataBind, I get the same invalid cast error again.

    How should I rectify this problem?

    Regards,
    Jacques
  10. Bodevain Svensson
    Bodevain Svensson avatar
    195 posts
    Member since:
    May 2006

    Posted 28 Aug 2009 Link to this post

    If I were in your shoes I would check whether the insert operation is completed successfully and boolean value (true or false) is entered for the new grid item in the hasValue source column. If that column allows null values and such is entered, the binding expression will fail. Also check the second section of this online doc.

     

    Bodevain 

  11. Truong Pham
    Truong Pham avatar
    15 posts
    Member since:
    Aug 2009

    Posted 28 Aug 2009 Link to this post

    I applied the guide from this post but my problem still the same. My dataflied is date field. Is there any solutions?
  12. Jacques
    Jacques avatar
    135 posts
    Member since:
    Jun 2007

    Posted 28 Aug 2009 Link to this post

    Hi Truong,

    Our scenario was boolean specific, but perhaps you can use this solution.

    We used method binding instead so in the HTML you would see the following:
    <asp:CheckBox ID="IsEnabled" runat="server" Text="(check to enable item)" Checked='<%# GetDefaultBool(Eval("IsEnabled")) %>' /> 


    For this to work you would also require the supporting method which looks like this:
    protected bool GetDefaultBool(object input) 
        if (input == null || input.ToString() == "" || input.ToString().ToLower() == "false"
        { 
            return false
        } 
        else 
        { 
            return true
        } 
    }  


    In your case I suspect you could replace the boolean types with Date types.

    Regards,
    Jacques






  13. Truong Pham
    Truong Pham avatar
    15 posts
    Member since:
    Aug 2009

    Posted 28 Aug 2009 Link to this post

    Dear Jaques,

    I have try as your guide but my problem npt solved . I use following function:

    Protected

     

    Function GetDefaultDate(ByVal input As Object) As Date

     

     

    If (input.Equals(DBNull.Value)) Then

     

     

    Return Date.Now

     

     

    Else

     

     

    Return input

     

     

    End If

     

     

    End Function

    Please help me on this issue

     

  14. Jacques
    Jacques avatar
    135 posts
    Member since:
    Jun 2007

    Posted 28 Aug 2009 Link to this post

    Hi Truong,

    Instead of using this:
    If (input.Equals(DBNull.Value)) Then  

    Rather use this:
    If (input Is Nothing Or input.ToString = ""Then   


    See if that works.

    Regards,
    Jacques


  15. Michael
    Michael avatar
    84 posts
    Member since:
    May 2008

    Posted 23 Sep 2009 Link to this post

    Hi Guys

    I am having this same issue and it is absolutely pissing me off!!

    I am using autoinserts on a popup template formeditor.

    When I click to add a new record, the checkbox Bind("Active") always throws an error. My data object always defaults "Active" with a false and I still get this error.

    I tried ItemCommand however it is not firing no matter what I do. It is not even entering this function obviously! I put it on the radgrid OnItemCommand method.

    Can anyone help with this?

    Thanks.
  16. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 24 Sep 2009 Link to this post

    Hi Michael,

    Generally "specified cast is not valid" exception will be raised only if the value of Active is not boolean.

    Best wishes,
    Vlad
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  17. Ryan C
    Ryan C avatar
    28 posts
    Member since:
    Aug 2009

    Posted 22 Oct 2009 Link to this post

    I too got a similar problem, that only on insert I would get  an error (on edit its ok).
    I have nothing in my asp.cs so Princy's solution doesn't apply and I tried J.Hov solution but get an error on edit, instead of insert.
    Any other solutions?

        <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Sunset" GridLines="None" AllowPaging="True" 
            PageSize="20" AllowSorting="True" AutoGenerateColumns="False" ShowStatusBar="true" 
            AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" 
            DataSourceID="sdsPolls" HorizontalAlign="NotSet" AllowMultiRowEdit="true">  
            <MasterTableView CommandItemDisplay="TopAndBottom" DataSourceID="sdsPolls" DataKeyNames="id" 
                EditMode="PopUp">  
     
     
    <asp:CheckBox ID="cbActive" runat="server" Checked='<%# Bind("active") %>' /> 
     
  18. Steve
    Steve avatar
    84 posts
    Member since:
    Aug 2013

    Posted 12 Jan 2014 Link to this post

    This is truly amazing that this is still an issue today, many years later! When the hell is Telerik going to fix this issue with their controls? I have seen numerous posts all over the web regarding DBNull values and checkboxes upon initiating a form insert with the Bind statement.

    Come on guys, fix this already!!!

    I'm having the same issue with a RadButton control that has a ToggleType="Checkbox".

    This is insane already. FIX IT!!!!!!!!!!!!!!!!!!!!!!

  19. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 15 Jan 2014 Link to this post

    Hi Steve,

    Please try setting the following code for Checked property for a RadButton to avoid the Exceptions being raised when the DB value is null:

    ASPX:
    <telerik:RadButton ID="IsTrue" runat="server" ButtonType="ToggleButton" ToggleType="CheckBox"
        Checked='<%# (DataBinder.Eval(Container.DataItem,"IsTrue") is DBNull ?false:Eval("IsTrue")) %>'>
        <ToggleStates>
            <telerik:RadButtonToggleState Value="True" />
            <telerik:RadButtonToggleState Value="False" />
        </ToggleStates>
    </telerik:RadButton>

    Thanks,
    Princy
  20. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 15 Jan 2014 Link to this post

    Hello Steve,

    Indeed I verified that the described issue exists into current version of the controls. However I will forward it to our developers for further investigation. Also I logged it into our public bug tracking system and I will increase its priority. On the following link you can find the public item and you can vote for it:
    http://feedback.telerik.com/Project/108/Feedback/Details/98747-fix-specified-cast-is-not-valid-error-is-thrown-when-radgrid-contains-checkbox
    Meantime in order to avoid the problem you can use the approach provided from Princy.

    Regards,
    Radoslav
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  21. Eric
    Eric avatar
    1 posts
    Member since:
    Feb 2011

    Posted 05 Sep 2014 in reply to Radoslav Link to this post

    the issue is that the unique id does not follow the check box all the way to the control level...

    <td id="ctl00_ContentPlaceHolder1_rgMaint_ctl00_ctl07_EditCell__Active"><input id="ctl00_ContentPlaceHolder1_rgMaint_ctl00_ctl07_ctl09" type="checkbox" name="ctl00$ContentPlaceHolder1$rgMaint$ctl00$ctl07$ctl09" checked="checked"></td>

    The control name is ctl09 when it should be ck_Active or simular
  22. Rama
    Rama avatar
    4 posts
    Member since:
    Aug 2015

    Posted 07 Aug 2015 in reply to Radoslav Link to this post

    Problem displaying checkbox value in template EditFormType insert mode in RadGrid. 

    I have a webpage displaying a list of departments on left side of page and details about the selected department on rightside. Each department has some general information and department contacts shown on two seperate tabs.

    We can add a new contact by clicking the 'Add New' button of RadGrid and filling enough information to insert. If we leave the insert form expanded and select a different department and click on second tab to see the contacts, I'm getting the error "Specified cast is not valid." I am using the ObjectDataSource to fill the RadGrid.

    Is there any solution to fix this? Any plan to fix this "http://feedback.telerik.com/Project/108/Feedback/Details/98747-fix-specified-cast-is-not-valid-error-is-thrown-when-radgrid-contains-checkbox"

    Thanks,

    Rama.

     

  23. Angel Petrov
    Admin
    Angel Petrov avatar
    1006 posts

    Posted 12 Aug 2015 Link to this post

    Hi Rama,

    As a solution you can try to conditionally bind a value to the check property as demonstrated below.

    ASPX:
    <asp:CheckBox runat="server" ID="CheckBox1" Checked='<%#(Container is GridEditFormInsertItem) ? false : DataBinder.Eval(Container.DataItem,"IsTrue")%> '/>

    Additionally we have planned to research whether we can improve the functionality. If possible we will alter the binding so that an exception is not raised for the insert item. You can monitor our progress from here.

    Regards,
    Angel Petrov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  24. Rama
    Rama avatar
    4 posts
    Member since:
    Aug 2015

    Posted 18 Aug 2015 in reply to Angel Petrov Link to this post

    I have checkbox as below:

     <asp:CheckBox ID="chkActive" CssClass="checkbox" runat="server" Text="Active"
                                        Checked='<%# Bind( "Active" ) %>'  />

    As per your suggestion, i have changed it to 

     <asp:CheckBox ID="chkActive" CssClass="checkbox" runat="server" Text="Active"
                                        Checked='<%#(Container is GridEditFormInsertItem) ? true :  DataBinder.Eval(Container.DataItem,"Active")%> '  />​​

    With this change, i am not getting the error "specificed cast is not valid" in insert mode. But this makes to send incorrect values to Update method while editing the item. Please help, thanks in advance.

    note: i am using ObjectDataSource to bind the grid, Insert,Update,Delete commands.

    Thanks,Rama.

  25. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 21 Aug 2015 Link to this post

    Hello Rama,

    Currently to achieve the desired functionality and to avoid errors you need to use the approach provided by Princy in earlier posts. The other option is to use the GridCheckBoxColumn which is designed for such cases.

    If you need further assistance, do not hesitate to contact us again.

    Regards,
    Radoslav
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  26. Rama
    Rama avatar
    4 posts
    Member since:
    Aug 2015

    Posted 21 Aug 2015 in reply to Radoslav Link to this post

    We used the same approach provided by Princy. I'm still having the error "Specified cast is not valid." It happens if i click "ADD NEW" and then (leaving the insert item form expanded on grid) select other item and try accessing the tab with that grid.

     Department1

     

  27. Rama
    Rama avatar
    4 posts
    Member since:
    Aug 2015

    Posted 21 Aug 2015 in reply to Rama Link to this post

    Please see my query posted on  07 Aug.

    We used the same approach provided by Princy. I'm still having the error "Specified cast is not valid." It happens if i click "ADD NEW" and then (leaving the insert item form expanded on grid) select other item and try accessing the tab with that grid.

    Thanks,

    Rama

  28. HaBo
    HaBo avatar
    22 posts
    Member since:
    Oct 2011

    Posted 18 Mar in reply to esaracing Link to this post

    if you have follwed "Princy"'s solution, you need to add a attribute to your grid OnItemCommand="RadGrid1_ItemCommand" then that workaround will work.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017