This is a migrated thread and some comments may be shown as answers.

problem displaying bool value in formtemplate type edit form insert mode

26 Answers 604 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sf
Top achievements
Rank 1
sf asked on 29 Jun 2009, 09:49 AM
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

26 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 Jun 2009, 10:42 AM
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
0
sf
Top achievements
Rank 1
answered on 30 Jun 2009, 06:45 AM
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?

0
Accepted
Princy
Top achievements
Rank 2
answered on 01 Jul 2009, 12:45 PM
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.
0
sf
Top achievements
Rank 1
answered on 02 Jul 2009, 04:27 AM
thanks Princy for your help, problem solved! 
0
esaracing
Top achievements
Rank 1
answered on 11 Aug 2009, 05:50 AM
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,



0
Vlad
Telerik team
answered on 12 Aug 2009, 02:14 PM
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.
0
Jacques
Top achievements
Rank 2
answered on 28 Aug 2009, 11:52 AM
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
0
Bodevain Svensson
Top achievements
Rank 1
answered on 28 Aug 2009, 12:41 PM

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 

0
Truong Pham
Top achievements
Rank 1
answered on 28 Aug 2009, 03:20 PM
I applied the guide from this post but my problem still the same. My dataflied is date field. Is there any solutions?
0
Jacques
Top achievements
Rank 2
answered on 28 Aug 2009, 03:52 PM
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






0
Truong Pham
Top achievements
Rank 1
answered on 28 Aug 2009, 04:27 PM
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

 

0
Jacques
Top achievements
Rank 2
answered on 28 Aug 2009, 04:51 PM
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


0
Michael
Top achievements
Rank 1
answered on 23 Sep 2009, 07:34 AM
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.
0
Vlad
Telerik team
answered on 24 Sep 2009, 01:08 PM
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.
0
Ryan C
Top achievements
Rank 1
answered on 22 Oct 2009, 02:40 PM
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") %>' /> 
 
0
Steve
Top achievements
Rank 1
answered on 12 Jan 2014, 02:21 PM

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

0
Princy
Top achievements
Rank 2
answered on 15 Jan 2014, 06:49 AM
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
0
Radoslav
Telerik team
answered on 15 Jan 2014, 07:51 AM
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.
0
Eric
Top achievements
Rank 1
answered on 05 Sep 2014, 05:22 PM
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
0
Rama
Top achievements
Rank 1
answered on 07 Aug 2015, 05:29 PM

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.

 

0
Angel Petrov
Telerik team
answered on 12 Aug 2015, 08:28 AM
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
0
Rama
Top achievements
Rank 1
answered on 18 Aug 2015, 06:26 PM

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.

0
Radoslav
Telerik team
answered on 21 Aug 2015, 07:46 AM
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
0
Rama
Top achievements
Rank 1
answered on 21 Aug 2015, 02:13 PM

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

 

0
Rama
Top achievements
Rank 1
answered on 21 Aug 2015, 02:25 PM

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

0
HaBo
Top achievements
Rank 1
answered on 18 Mar 2016, 03:02 PM
if you have follwed "Princy"'s solution, you need to add a attribute to your grid OnItemCommand="RadGrid1_ItemCommand" then that workaround will work.
Tags
Grid
Asked by
sf
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
sf
Top achievements
Rank 1
esaracing
Top achievements
Rank 1
Vlad
Telerik team
Jacques
Top achievements
Rank 2
Bodevain Svensson
Top achievements
Rank 1
Truong Pham
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Ryan C
Top achievements
Rank 1
Steve
Top achievements
Rank 1
Radoslav
Telerik team
Eric
Top achievements
Rank 1
Rama
Top achievements
Rank 1
Angel Petrov
Telerik team
HaBo
Top achievements
Rank 1
Share this question
or