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

[Solved] Invalid Cast Exception using Custom Edit Form in Grid

2 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ismail
Top achievements
Rank 1
Ismail asked on 01 Jul 2009, 09:47 AM
HI
I have the following code for a grid that i'm using to manage Users. I created a custom edit form with a few textobxes and a checkbox, this works 100% when editing an item, however i get an error(Invalid Cast Exception) when trying to create a new User, this error is thrown while the custom form is being loaded.
I know that this is caused by the asp checkbox that i'm using in the edit form, Please assist me.
Thanks in advance :)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdminPage.aspx.cs" Inherits="AddEditUsers" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>All Users</title> 
    <style type="text/css"
        .style1 
        { 
            width: 100%; 
            height: 68px; 
        } 
    </style> 
</head> 
<body> 
     
    <form id="form1" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    </asp:ScriptManager> 
    <div> 
    <table class="style1"
        <tr> 
            <td> 
                &nbsp;</td> 
        </tr> 
        <tr> 
            <td> 
                <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"  
                    DataSourceID="SqlDataSource1" GridLines="None"  
                    Skin="Vista" AllowAutomaticDeletes="True" AllowAutomaticUpdates="true"  
                    AllowAutomaticInserts="true" AllowMultiRowEdit="True"
<MasterTableView datakeynames="UserId" datasourceid="SqlDataSource1" EditMode="PopUp"  commanditemdisplay="Top" > 
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridButtonColumn ConfirmText="Are You Sure You Want To Delete This User?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1"
                                        <HeaderStyle Width="20px" /> 
                                        <ItemStyle HorizontalAlign="Center" /> 
        </telerik:GridButtonColumn> 
        <telerik:GridBoundColumn DataField="UserId" DataType="System.Int32"  
            HeaderText="UserId" ReadOnly="True" SortExpression="UserId" UniqueName="UserId" Visible="false"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name"  
            SortExpression="UserName" UniqueName="UserName"
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="Password" HeaderText="Password"  
            SortExpression="Password" UniqueName="Password"
        </telerik:GridBoundColumn> 
        <telerik:GridCheckBoxColumn DataField="IsAdmin" HeaderText="Administrator" 
                        DataType="System.Boolean"
                    </telerik:GridCheckBoxColumn> 
        <telerik:GridEditCommandColumn ButtonType="ImageButton"
        </telerik:GridEditCommandColumn> 
        <telerik:GridHyperLinkColumn></telerik:GridHyperLinkColumn> 
         
    </Columns> 
     <EditFormSettings EditFormType="Template"
     <EditColumn UniqueName="EditCommandColumn1"></EditColumn> 
     <FormTemplate> 
     <table id="EditTable" cellspacing="1" cellpadding="1" border="0"
     <tr>  
        <td>User Name</td> 
        <td> <asp:TextBox ID="txtUserName" runat="server" MaxLength="10" Text='<%#Bind("UserName") %>'></asp:TextBox></td
     </tr> 
     <tr> 
        <td>Password</td> 
        <td> <asp:TextBox ID="txtPassword" runat="server" MaxLength="15" Text='<%#Bind("Password") %>'></asp:TextBox></td
     </tr> 
     <tr> 
        <td></td
        <td> <asp:CheckBox ID="chkIsAdmin" runat="server" Text="Administrator" Checked='<%#Bind("IsAdmin") %>'></asp:CheckBox></td
     </tr> 
     <tr> 
        <td><asp:Button id="btnUpdate" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'/></td> 
        <td><asp:Button ID="btnCancel" runat="server" CausesValidation="false" CommandName="cancel" Text="Cancel"/></td> 
     </tr> 
     </table> 
     </FormTemplate> 
     </EditFormSettings> 
</MasterTableView> 
 
                    <ClientSettings> 
                        <Selecting AllowRowSelect="True" /> 
                    </ClientSettings> 
 
<FilterMenu EnableTheming="True"
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
</FilterMenu> 
                </telerik:RadGrid> 
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
                    ConflictDetection="CompareAllValues"  
                    ConnectionString="<%$ ConnectionStrings:lawsocConnectionString %>"  
                    DeleteCommand="DELETE FROM [tbUsers2] WHERE [UserId] = @original_UserId AND [UserName] = @original_UserName AND [Password] = @original_Password AND [IsAdmin] = @original_IsAdmin"  
                    InsertCommand="INSERT INTO [tbUsers2] ([UserName], [Password], [IsAdmin]) VALUES (@UserName, @Password, @IsAdmin)"  
                    OldValuesParameterFormatString="original_{0}"  
                    SelectCommand="SELECT [UserId], [UserName], [Password], [IsAdmin] FROM [tbUsers2]"  
                    UpdateCommand="UPDATE [tbUsers2] SET [UserName] = @UserName, [Password] = @Password, [IsAdmin] = @IsAdmin WHERE [UserId] = @original_UserId AND [UserName] = @original_UserName AND [Password] = @original_Password AND [IsAdmin] = @original_IsAdmin"
                    <DeleteParameters> 
                        <asp:Parameter Name="original_UserId" Type="Int32" /> 
                        <asp:Parameter Name="original_UserName" Type="String" /> 
                        <asp:Parameter Name="original_Password" Type="String" /> 
                        <asp:Parameter Name="original_IsAdmin" Type="String" /> 
                    </DeleteParameters> 
                    <UpdateParameters> 
                        <asp:Parameter Name="UserName" Type="String" /> 
                        <asp:Parameter Name="Password" Type="String" /> 
                        <asp:Parameter Name="IsAdmin" Type="String" /> 
                        <asp:Parameter Name="original_UserId" Type="Int32" /> 
                        <asp:Parameter Name="original_UserName" Type="String" /> 
                        <asp:Parameter Name="original_Password" Type="String" /> 
                        <asp:Parameter Name="original_IsAdmin" Type="String" /> 
                    </UpdateParameters> 
                    <InsertParameters> 
                        <asp:Parameter Name="UserName" Type="String" /> 
                        <asp:Parameter Name="Password" Type="String" /> 
                        <asp:Parameter Name="IsAdmin" Type="String" /> 
                    </InsertParameters> 
                </asp:SqlDataSource> 
            </td> 
        </tr> 
        <tr> 
            <td> 
                &nbsp;</td> 
        </tr> 
        <tr> 
            <td> 
                &nbsp;</td> 
        </tr> 
    </table> 
    </div> 
    </form> 
</body> 
</html> 
 
 







2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Jul 2009, 11:35 AM

Hello Ismail,

The reason for this error is that your grid instance can not bind a checkbox value for the newly inserted item through theEval()/Bind() syntax you hard-coded.This can be fixed by presetting the default value of the checkbox when binding to a grid item on the RadGrid.InitInsertCommandName command as shown below:

c#:

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["IsAdmin"] = false
 
        //Insert the item and rebind 
        e.Item.OwnerTableView.InsertItem(newValues); 
    } 
 

Thanks

Shinu.

0
Ismail
Top achievements
Rank 1
answered on 01 Jul 2009, 11:53 AM
Hi
Thanx alot,i apperciate you response, how do i get this event to be raised, does it happen automatically or do i have to put coded in place to raise this event..

Thank you very much
Tags
Grid
Asked by
Ismail
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ismail
Top achievements
Rank 1
Share this question
or