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

RadGrid InsertCommand with CheckBox Error

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 22 Jan 2015, 05:13 PM
I have a client side data bind on my RadGrid. The Delete and Update Commands work perfectly, but when I try to insert a new row my code breaks at the Checkbox bind with the following: "Specified cast is not valid." I am new to C#, Asp, and Telerik controls, any help would be greatly appreciated.
Here is my Page Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidationEdit.aspx.cs" Inherits="EditPages_ValidationEdit" MasterPageFile="~/EUEMain.master" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="cphLeftNav" runat="Server">
    <asp:HyperLink ID="HomeLink" runat="server" NavigateUrl="..\Default.aspx" Text="Home" />
    <br />
    <asp:HyperLink ID="AddLink" runat="server" Text="Status Edit Page" />
    <br />
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">
 
    <h1>Subdivision Statuses</h1>
 
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
    <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#FF8080">
    </asp:Label>
    <asp:Label ID="Label2" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="#00C000">
    </asp:Label>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
 
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
 
    <div id="Testing">
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSourceSub" AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
            AllowAutomaticDeletes="true" AllowSorting="true" AutoGenerateColumns="False" OnItemUpdated="RadGrid1_ItemUpdated"
            OnPreRender="RadGrid1_PreRender" OnItemInserted="RadGrid1_ItemInserted">
            <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="TopAndBottom" DataKeyNames="StatusId" DataSourceID="SqlDataSourceSub">
                <Columns>
                    <telerik:GridEditCommandColumn>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="StatusID" HeaderText="Status ID" UniqueName="StatusID" Visible="False" />
                    <telerik:GridBoundColumn DataField="StatusType" HeaderText="Status Type" UniqueName="StatusType" Visible="False" />
                    <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description" />
                    <telerik:GridCheckBoxColumn DataField="Visible" HeaderText="Visible" UniqueName="Visible" />
                    <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column" />
                </Columns>
                <EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <table id="table1">
                            <tr>
                                <td>Subdivision Status Details</td>
                            </tr>
                            <tr>
                                <td>
                                    <table id="Table2">
                                        <tr>
                                            <td >Status ID</td>
                                            <td>
                                                <asp:Label runat="server" ID="StatusIdLabel" Text='<%#Bind("StatusId") %>' Visible="False"></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Status Type</td>
                                            <td>
                                                <asp:TextBox runat="server" ID="StatusTypeTextBox" Text='<%#Bind("StatusType") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td runat="server">Description</td>
                                            <td>
                                                <asp:TextBox runat="server" ID="DescriptionTextBox" Text='<%#Bind("Description") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Visible</td>
                                            <td>
                                                <asp:CheckBox  Checked='<%#Bind("Visible") %>' ID="VisibleCheckBox" runat="server" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                        CommandName="Cancel"></asp:Button>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
 
    <asp:SqlDataSource ID="SqlDataSourceSub" runat="server" ConnectionString="<%$ ConnectionStrings:PtcDbModelEntities %>"
        DeleteCommand="DELETE FROM [dbo].[Status] WHERE [StatusId] = @StatusId"
        InsertCommand="INSERT INTO [dbo].[Status] ([StatusType], [Description], [Visible], VALUES (@StatusType, @Description, @BitVisible)"
        SelectCommand="SELECT [StatusId], [StatusType], [Visible], [Description] FROM [dbo].[Status] where StatusType = 'S'"
        UpdateCommand="UPDATE [dbo].[Status] SET [StatusType] = @StatusType, [Description] = @Description,  Visible = @Visible  WHERE [StatusId] = @StatusId">
        <DeleteParameters>
            <asp:Parameter Name="StatusId" Type="Int32"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="StatusType" Type="String"></asp:Parameter>
            <asp:Parameter Name="Description" Type="String"></asp:Parameter>
            <asp:Parameter Name="Visible" Type="Boolean"></asp:Parameter>
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="StatusType" Type="String"></asp:Parameter>
            <asp:Parameter Name="Description" Type="String"></asp:Parameter>
            <asp:Parameter Name="Visible" Type="Boolean"></asp:Parameter>
            <asp:Parameter Name="StatusId" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
and my code Behind (C#):
using System;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
public partial class EditPages_ValidationEdit : BNSF.EUECommonApplication.EUEBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddLink.NavigateUrl = @"..\EditPages\ValidationEdit.aspx";
    }
    protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        SqlDataSourceSub.UpdateParameters["Description"].DefaultValue = e.ToString();
        SqlDataSourceSub.UpdateParameters["StatusType"].DefaultValue = e.ToString();
        SqlDataSourceSub.UpdateParameters["Visible"].DefaultValue = e.ToString();
 
        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            DisplayMessage(true, "Status " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["StatusId"] + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Status " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["StatusId"] + " updated");
        }
    }
 
    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
    {
         
        SqlDataSourceSub.InsertParameters["StatusType"].DefaultValue = e.ToString();
        SqlDataSourceSub.InsertParameters["Description"].DefaultValue = e.ToString();
        SqlDataSourceSub.InsertParameters["Visible"].DefaultValue = e.ToString();
    }
 
    protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            DisplayMessage(true, "Status " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["StatusId"] + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "Status " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["StatusId"] + " deleted");
        }
    }
 
    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            editColumn.Visible = false;
        }
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
        {
            e.Canceled = true;
        }
        else
        {
            GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            if (!editColumn.Visible)
                editColumn.Visible = true;
        }
    }
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RadGrid1.EditIndexes.Add(0);
            RadGrid1.Rebind();
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 22 Jan 2015, 08:35 PM
got it: when you try to add a new record and there is a bool value present you need to set the DefaultInsertValue in the gridcheckboxcolumn 







when you try to add a new record and there is a bool value present
you need to set the DefaultInsertValue in the gridcheckboxcolumn

Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Share this question
or