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

RadGrid ItemUpdated error; object cannot be cast from DBnull to other types

7 Answers 277 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sheepdog
Top achievements
Rank 1
Sheepdog asked on 21 Nov 2008, 08:24 PM
I am using the latest Ajax Radgrid I have bound it to a SQLdatasource. I have stored procs for the update and delete methods of the gridrow. I am not allowing new records/inserts. The grid is also grouped by one field.

The problem I am having is the record may not exist yet in the database, so the datakey field may be null. But that is ok, in the stored proc if I see a null datakey field I assume its a new record and do the insert, otherwise I do an update.

So I click the edit button on the row, the boxes open up so I can enter data, then click the update link and BOOM. I get an error saying that the Object cannot be cast from DBNull to other types. Looking in the code behind page it blows up somewhere after the start of the RadGrid_ItemUpdated Event and before the SQLdatasource_updating event.

I need to be able to pass null values to the stored proc, even if it is the datakey field.

Thanks
G

7 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 24 Nov 2008, 04:12 PM
Hi Sheepdog,

Indeed the ItemUpdated event of the grid is fired after the SqlDataSource Updating and Updated events.
Could you please send us your grid and SqlDataSource control declaration so I could check it out and see if there is something incorrect there?

Additionally, you can review the following online resources for more information about RadGrid lifecycle and automatic operations:
http://www.telerik.com/help/aspnet-ajax/grdeventsequence.html
http://www.telerik.com/help/aspnet-ajax/grdautomaticdatasourceoperations.html

Let me know how it goes.

Kind regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sheepdog
Top achievements
Rank 1
answered on 26 Nov 2008, 04:03 PM
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="NSCStoreTaskList.ascx.vb" Inherits="NSCTaskListMaint.NSCStoreTaskList" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
</telerik:RadScriptManager> 
<asp:Literal ID="litStorenbr" runat="server" Text="0" Visible="False"></asp:Literal>&nbsp;  
<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:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True"   
AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AutoGenerateColumns="False"   
AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" DataSourceID="SqlDataSource1"   
GridLines="None" SkinID="Default" ShowGroupPanel="True">  
    <HeaderContextMenu EnableTheming="True">  
        <CollapseAnimation Duration="200" Type="OutQuint" /> 
    </HeaderContextMenu> 
    <MasterTableView AllowAutomaticInserts="False" 
    DataKeyNames="lngtasklistID" 
        DataSourceID="SqlDataSource1" EditMode="InPlace"   
        EnableHeaderContextMenu="True" HierarchyDefaultExpanded="True">  
        <RowIndicatorColumn> 
            <HeaderStyle Width="20px" /> 
        </RowIndicatorColumn> 
        <ExpandCollapseColumn> 
            <HeaderStyle Width="20px" /> 
        </ExpandCollapseColumn> 
        <Columns> 
            <telerik:GridBoundColumn DataField="lngTasklistID" DataType="System.Int32" Display="False" 
                HeaderText="lngTasklistID" ReadOnly="True" SortExpression="lngTasklistID" UniqueName="lngTasklistID" Visible="False" ConvertEmptyStringToNull="False">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="lngPeriodID" DataType="System.Int32" Display="False" 
                HeaderText="lngPeriodID" SortExpression="lngPeriodID" UniqueName="lngPeriodID" Visible="False" ConvertEmptyStringToNull="False" ReadOnly="True">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="lngStoreID" DataType="System.Int32" Display="False" 
                HeaderText="lngStoreID" ReadOnly="True" SortExpression="lngStoreID" UniqueName="lngStoreID" Visible="False" ConvertEmptyStringToNull="False">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="lngTasklistPeriod" DataType="System.Int32" HeaderText="Task list Period" 
                ReadOnly="True" SortExpression="lngTasklistPeriod"  UniqueName="lngTasklistPeriod">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="txtTasklistDesc" HeaderText="txtTasklistDesc" 
                ReadOnly="True" SortExpression="txtTasklistDesc" UniqueName="txtTasklistDesc">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="dteDueDate" DataType="System.DateTime" HeaderText="dteDueDate" 
                SortExpression="dteDueDate" UniqueName="dteDueDate">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="txtNotes" HeaderText="txtNotes" SortExpression="txtNotes" 
                UniqueName="txtNotes">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="dteDateCompleted" DataType="System.DateTime" 
                HeaderText="dteDateCompleted" SortExpression="dteDateCompleted" UniqueName="dteDateCompleted">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="txtCompletedBy" HeaderText="txtCompletedBy" SortExpression="txtCompletedBy" 
                UniqueName="txtCompletedBy">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="txtLastUpdatedBy" HeaderText="txtLastUpdatedBy" 
                SortExpression="txtLastUpdatedBy" UniqueName="txtLastUpdatedBy">  
            </telerik:GridBoundColumn> 
        </Columns> 
        <GroupByExpressions> 
            <telerik:GridGroupByExpression> 
                <SelectFields> 
                    <telerik:GridGroupByField FieldAlias="Period" FieldName="lngTaskListPeriod" FormatString="" HeaderText="" ></telerik:GridGroupByField> 
                      
                </SelectFields> 
                <GroupByFields> 
                    <telerik:GridGroupByField FieldName="lngTaskListPeriod" SortOrder="Descending" FieldAlias="lngTaskListPeriod" FormatString="" HeaderText="" /> 
                </GroupByFields> 
            </telerik:GridGroupByExpression> 
        </GroupByExpressions> 
    </MasterTableView> 
    <FilterMenu EnableTheming="True">  
        <CollapseAnimation Duration="200" Type="OutQuint" /> 
    </FilterMenu> 
</telerik:RadGrid><br /> 
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Height="75px" 
    Width="75px">  
    <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' 
        style="border: 0px;" /> 
</telerik:RadAjaxLoadingPanel> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FFEConnectionString %>" 
    DeleteCommand="spDEL_NSCStoreTask" DeleteCommandType="StoredProcedure" SelectCommand="SELECT T1.lngTasklistID, T1.lngStoreID, P1.txtTasklistDesc, T1.dteDueDate  
    , T1.txtNotes, T1.dteDateCompleted, T1.txtCompletedBy, T1.txtLastUpdatedBy, P1.lngPeriodID, P1.lngTasklistPeriod    
    FROM dbo.tblNSCStoreTaskList T1 RIGHT OUTER JOIN dbo.tblNSCTasklistPeriods P1 ON T1.lngPeriodID = P1.lngPeriodID   
    AND T1.lngStoreID = @Param1"  
    UpdateCommand="spUPDT_NSCStoreTask" UpdateCommandType="StoredProcedure">  
    <DeleteParameters> 
        <asp:Parameter Name="lngTasklistID" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
        <asp:Parameter Name="lngTasklistID" Type="Int32"  DefaultValue="0"  /> 
        <asp:Parameter Name="dteDueDate" Type="DateTime" /> 
        <asp:Parameter Name="txtNotes" Type="String" /> 
        <asp:Parameter Name="dteDateCompleted" Type="DateTime" /> 
        <asp:Parameter Name="txtCompletedBy" Type="String" /> 
        <asp:Parameter Name="txtLastUpdatedBy" Type="String" /> 
    </UpdateParameters> 
     
    <SelectParameters> 
        <asp:ControlParameter ControlID="litStorenbr" Name="Param1" PropertyName="Text" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
 

Here is the code I am trying to use. There is nothing of any value on the codebehind page. So I am not including it.
Mainly my problem is an error I get when I try to update a row, saying that it cannot cast a dbnull to any other type.
0
Iana Tsolova
Telerik team
answered on 27 Nov 2008, 02:08 PM
Hello Sheepdog,

Please try naming the Update/Delete/Select parameters in your SqlDataSource control with the same name as the corresponding DataField is called and check if it makes any difference.

Best wishes,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sheepdog
Top achievements
Rank 1
answered on 01 Dec 2008, 12:47 PM
Naming them the same as the datafields? what are you talking about? They are named the same! Even the stored procedure parameters fields are named the same. If you could point out what is not named the same? perhaps I missed one, but I just looked them over again and they look all the same.

Thanks
0
Sheepdog
Top achievements
Rank 1
answered on 01 Dec 2008, 01:37 PM
I have done some looking into the debugger, and looking at the stack trace it seems the error is being thrown because it is trying to cast the lngTaskListID which is null, to int32. Because this is the datakeyfield I think the update routine is expecting a value. And because I am doing inserts and updates with the update code, I think this is where I am having the trouble.

I need to allow an update to continue if the datakeyfield is null/empty. I will deal with this in the stored procedure.

So some how I need to convert this null value to a zero before it gets to the sqldatasource update event.

Thanks
G
0
Iana Tsolova
Telerik team
answered on 01 Dec 2008, 02:48 PM
Hi Sheepdog,

Can you try handling the InsertCommand event of the grid as set there the lngTasklistID parameter accordingly?

Find more on how to handle RadGrid insertion here.

Let me know how it goes.

Best wishes,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sheepdog
Top achievements
Rank 1
answered on 01 Dec 2008, 06:41 PM
OK,

First I am not using an InsertCommand to do the insert, I am forcing the UpdateCommand to do updates and inserts. So your answer wont work as I need to deal with the Updatecommand, which is what I resorted to using.

So I figured it out on my own. Thanks for trying
Tags
Grid
Asked by
Sheepdog
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Sheepdog
Top achievements
Rank 1
Share this question
or