26 Answers, 1 is accepted
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
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); |
} |
} |
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.
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:
|
thnx in advance,
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.
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
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
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
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
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
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.
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.
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") %>' /> |
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!!!!!!!!!!!!!!!!!!!!!!
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
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
<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
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.
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
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.
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
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
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