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

RadGrid GridButtonColumn Delete returns 'Object reference not set to an instance of an object'

3 Answers 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason Sheats
Top achievements
Rank 1
Jason Sheats asked on 02 Feb 2012, 05:28 PM

 

 

Please note that I do not have a TemplateColumn in my grid to adding a blank ItemTemplate will not work.  I read the forums until I am tired of doing it.  If I refresh the grid after clearing the IE8 error popup, the record is deleted.  I get the prompt, and the record gets deleted, but I simply must know how to get rid of this stupid message.

I'm guessing it's something with databinding, but I have no idea what.


Please help if possible.  I appreciate the help in advance.

Jason

Datasource...

 

 

 

<asp:LinqDataSource ID="ldsDeviceToDeviceInfo" runat="server"

 

 

 

ContextTypeName="AssetManagementDataContext" EnableDelete="True"

 

 

 

EnableInsert="True" EnableUpdate="True" EntityTypeName=""

 

 

 

OrderBy="DeviceName, ConnType" TableName="Connections">

 

 

 

</asp:LinqDataSource>

I have this in my RadAjaxManager (among other things not related to this grid):

 

 

 

<telerik:AjaxSetting AjaxControlID="grdDevToDev">

 

 

 

<UpdatedControls>

 

 

 

<telerik:AjaxUpdatedControl ControlID="grdDevToDev" LoadingPanelID="RadAjaxLoadingPanel1" />

 

 

 

</UpdatedControls>

 

 

 

</telerik:AjaxSetting>

 

 

 

 

 

 

 

ASPX RadGrid declaration:

               <telerik:RadGrid ID="grdDevToDev" runat="server" EnableLinqExpressions="False" AllowFilteringByColumn="True"
                    AllowPaging="True" AllowSorting="True" CellSpacing="0"
                    DataSourceID="ldsDeviceToDeviceInfo" Font-Names="Verdana" Font-Size="8px" GridLines="None"
                    Skin="WebBlue" Width="90%" AutoGenerateColumns="False" 
                    onitemcommand="grdDevToDev_ItemCommand">
                        <ClientSettings AllowColumnsReorder="True" AllowKeyboardNavigation="true" ReorderColumnsOnClient="True"
                            EnablePostBackOnRowClick="false" >
                            <Selecting AllowRowSelect="True" />
                            <ClientEvents OnPopUpShowing="PopUpShowing" />
                        </ClientSettings>
                        <MasterTableView DataKeyNames="Index"  AllowAutomaticDeletes="true"
                            DataSourceID="ldsDeviceToDeviceInfo" EditMode="Popup" CommandItemDisplay="Top" >
                            <Columns>
                                <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="Index" DataType="System.Int64" 
                                    FilterControlAltText="Filter Index column" HeaderText="Index" 
                                    ReadOnly="True" SortExpression="Index" UniqueName="Index" Visible="false">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="DeviceName" 
                                    FilterControlAltText="Filter DeviceName column" HeaderText="DeviceName" 
                                    SortExpression="DeviceName" UniqueName="DeviceName">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ConnType" 
                                    FilterControlAltText="Filter ConnType column" HeaderText="ConnType" 
                                    SortExpression="ConnType" UniqueName="ModConnTypeel">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="CableType" 
                                    FilterControlAltText="Filter CableType column" HeaderText="CableType" 
                                    SortExpression="CableType" UniqueName="CableType">
                                    <HeaderStyle Width="150px" />
                                </telerik:GridBoundColumn>
                                <telerik:GridButtonColumn ConfirmText="Delete this record?" ConfirmDialogType="RadWindow"
                                    ConfirmTitle="Delete" ButtonType="LinkButton" CommandName="Delete" Text="Delete"
                                    UniqueName="DeleteColumn">
                                    <HeaderStyle Width="50" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </telerik:GridButtonColumn>
                            </Columns>
  
                            <EditFormSettings InsertCaption="Assign/Modify Device to Device Connection" CaptionFormatString="Edit Record: {0}"
                                CaptionDataField="Index" EditFormType="Template">
                                <PopUpSettings Modal="True" Width="500px" Height="500px" /> 
                                <FormTemplate>
                                    <table id="Table1" cellspacing="0" cellpadding="3" width="450" border="0">
                                        <tr>
                                            <td colspan="3" style="height: 50px">
                                                <asp:ValidationSummary ID="valSummaryNewDevice" ValidationGroup="SaveNewDevice" runat="server"
                                                    ForeColor="DarkRed" Width="670px" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <br />
                                            </td>
                                        </tr>
  
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td style="width: 150px; padding: 1px 2px 1px 2px">
                                                <asp:Label ID="Label3" runat="server" CssClass="formLabel" Height="20px" Text="ID"></asp:Label>
                                            </td>
                                            <td>
                                                 <asp:Label ID="tbIndex" Font-Names="Verdana" Font-Size="8pt" runat="server"
                                                    Text='<%# Bind("Index") %>' Width="50px" ></asp:Label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td style="width: 150px; padding: 1px 2px 1px 2px">
                                                <asp:Label ID="Label6" runat="server" CssClass="formLabel" Height="20px" Text="Device Name"></asp:Label>
                                            </td>
                                            <td>
                                                <asp:TextBox ID="tbAddDeviceName" Width="200px" Text='<%# Bind("DeviceName") %>' Font-Names="verdana" Font-Size="8pt" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="valNewDeviceSubmit" ControlToValidate="tbAddDeviceName"
                                                    SetFocusOnError="true" ValidationGroup="SaveNewDevice" Display="Dynamic" ErrorMessage="Missing Device Name"
                                                    runat="server">
                                                    <img id="Img5" src="~/images/flag_red.png" runat="server" alt="Missing data" /></asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td style="width: 150px; padding: 1px 2px 1px 2px">
                                                <asp:Label ID="Label7" runat="server" CssClass="formLabel" Height="20px" Text="Device Type"></asp:Label>
                                            </td>
                                            <td>
                                               <%-- <telerik:RadComboBox ID="rcbAddDeviceType" runat="server" Font-Names="Verdana" Font-Size="8pt" AllowCustomText="true" 
                                                    MarkFirstMatch="True" NoWrap="True" Skin="Windows7" SortCaseSensitive="False" SelectedValue='<%# Bind("Model") %>'
                                                    MaxHeight="250px" Width="200px" DataSourceID="dsDeviceTypes" DataTextField="Device_Type" DataValueField="Device_Type"
                                                    AutoPostBack="False"  AppendDataBoundItems="true">
                                                    <Items>
                                                        <telerik:RadComboBoxItem Text="" Value="" />
                                                    </Items>
                                                </telerik:RadComboBox>
                                                  
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="rcbAddDeviceType"
                                                    SetFocusOnError="true" ValidationGroup="SaveNewDevice" Display="Dynamic" ErrorMessage="Missing Device Type"
                                                    runat="server">
                                                    <img id="Img1" src="~/images/flag_red.png" runat="server" alt="Missing data" /></asp:RequiredFieldValidator>--%>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td>
                                                <asp:Label ID="Label8" runat="server" CssClass="formLabel" Height="20px" Text="Cabinet"></asp:Label>
                                            </td>
                                            <td>
<%--                                              <telerik:RadComboBox ID="rcbAddDeviceCabinet" runat="server" Font-Names="Verdana" AllowCustomText="true" 
                                                    Font-Size="8pt" MarkFirstMatch="True" NoWrap="True" AutoPostBack="False" Skin="Windows7"
                                                    SortCaseSensitive="False" MaxHeight="250px" Width="200px" DataSourceID="dsCabNums" SelectedValue='<%# Bind("Rack") %>'
                                                    DataTextField="CabinetNumber" DataValueField="CabinetNumber" AppendDataBoundItems="true"  >
                                                    <Items>
                                                        <telerik:RadComboBoxItem Text="" Value="" />
                                                    </Items>
                                                </telerik:RadComboBox>
                                                  
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="rcbAddDeviceCabinet"
                                                    SetFocusOnError="true" ValidationGroup="SaveNewDevice" Display="Dynamic" ErrorMessage="Missing Cabinet"
                                                    runat="server">
                                                    <img id="Img3" src="~/images/flag_red.png" runat="server" alt="Missing data" /></asp:RequiredFieldValidator>--%>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td style="width: 150px; padding: 1px 2px 1px 2px">
                                                <asp:Label ID="Label9" runat="server" CssClass="formLabel" Height="20px" Text="RU Position"></asp:Label>
                                            </td>
                                            <td>
<%--                                                <asp:TextBox ID="tbRUPosition" Text='<%# Bind("Rack_Position") %>' Width="200px" Font-Names="verdana" Font-Size="8pt" runat="server"></asp:TextBox><asp:RequiredFieldValidator
                                                    ID="RequiredFieldValidator3" ControlToValidate="tbRUPosition" SetFocusOnError="true"
                                                    ValidationGroup="SaveNewDevice" Display="Dynamic" ErrorMessage="Missing RU Position"
                                                    runat="server">
                                                    <img id="Img4" src="~/images/flag_red.png" runat="server" alt="Missing data" /></asp:RequiredFieldValidator>--%>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 20px;">
                                                   
                                            </td>
                                            <td colspan="2">
                                                <%--<hr />--%>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="3">
                                                <br />
                                            </td>
                                        </tr>
                                    </table>
                                    <table style="width: 100%">
                                        <tr>
                                            <td align="center" colspan="2">
                                                <table>
                                                    <tr>
                                                        <td>
                                                            <telerik:RadButton ID="Button1" ValidationGroup="SaveNewDevice" TabIndex="14" CausesValidation="true"
                                                                Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Save" %>' runat="server"
                                                                Skin="WebBlue" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                                            </telerik:RadButton>
                                                        </td>
                                                        <td>
                                                            <telerik:RadButton ID="btnCancel" CommandName="Cancel" TabIndex="15" Text="Cancel"
                                                                CausesValidation="False" runat="server" Skin="WebBlue">
                                                            </telerik:RadButton>
                                                        </td>
                                                </table>
                                        </tr>
                                        </td> </tr>
                                    </table>
                                </FormTemplate>
                    <EditColumn UniqueName="EditCommandColumn1">
                    </EditColumn>
                    <FormTableButtonRowStyle HorizontalAlign="Center" />
                </EditFormSettings>
                        </MasterTableView>
                    </telerik:RadGrid>



Only method in code-behind for this grid:
protected void grdDevToDev_ItemCommand(object sender, GridCommandEventArgs e)
{
    AssetManagementDataContext dc = new AssetManagementDataContext();
    if (e.CommandName == RadGrid.DeleteCommandName)
    {
        var index = (int)((GridDataItem)e.Item).GetDataKeyValue("Index");
        //retrieve entity form the Db
        var device = dc.Connections.Where(n => n.Index == index).FirstOrDefault();
        if (device != null)
        {
            //add the category for deletion
            dc.Connections.DeleteOnSubmit(device);
            try
            {
                //submit chanages to Db
                dc.SubmitChanges();
                grdAddSwitch.Rebind();
            }
            catch (System.Exception exc)
            {
                Response.Write("Error deleting switch.\nError Message: " + exc.Message);
            }
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Johny
Top achievements
Rank 1
answered on 06 Feb 2012, 10:32 AM
Hi

It looks like you have scenario similar to what telerik show here:

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/programaticlinqupdates/defaultcs.aspx

Difference i see is that they don't rebind their grid on command. May be you can try this.

Thanks,
Johny
0
Pat
Top achievements
Rank 1
answered on 25 Sep 2014, 01:53 PM
I also have a problem when deleting from my RadGrid control. I set the Data Source of the RadGrid to a basic EntityDataSource object. AutoGenerateDeleteColunm is True and AllowAutomaticDeletes is True. Everything works fine when the user clicks on the "delete" link. But as soon as I convert one the fields in the grid to a template colunm, the "delete" produces the following error: Object reference not set to an instance of an object. There is no custom event code on this page.

Here is the code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="PMC.Test1" %>
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.Core.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQuery.js">
</asp:ScriptReference>
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQueryInclude.js">
</asp:ScriptReference>
</Scripts>
</telerik:RadScriptManager>
<div>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=PMCEntities" DefaultContainerName="PMCEntities"
EnableDelete="True" EnableFlattening="False" EnableInsert="True"
EnableUpdate="True" EntitySetName="PMC_RunMiscParts">
</asp:EntityDataSource>
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True"
AllowAutomaticInserts="True" AllowAutomaticUpdates="True"
AutoGenerateColumns="False" AutoGenerateDeleteColumn="True"
AutoGenerateEditColumn="True" CellSpacing="0" DataSourceID="EntityDataSource1"
GridLines="None">
<MasterTableView datakeynames="RunMicsPartID" datasourceid="EntityDataSource1">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="RunMicsPartID" DataType="System.Int32"
FilterControlAltText="Filter RunMicsPartID column" HeaderText="RunMicsPartID"
ReadOnly="True" SortExpression="RunMicsPartID" UniqueName="RunMicsPartID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="RunID" DataType="System.Int32"
FilterControlAltText="Filter RunID column" HeaderText="RunID"
SortExpression="RunID" UniqueName="RunID">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="PartNum"
FilterControlAltText="Filter PartNum column" HeaderText="PartNum"
SortExpression="PartNum" UniqueName="PartNum">
<EditItemTemplate>
<asp:TextBox ID="PartNumTextBox" runat="server" Text='<%# Bind("PartNum") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="PartNumLabel" runat="server" Text='<%# Eval("PartNum") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="WorkOrder"
FilterControlAltText="Filter WorkOrder column" HeaderText="WorkOrder"
SortExpression="WorkOrder" UniqueName="WorkOrder">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>
</div>
</form>
</body>
</html>
0
Kostadin
Telerik team
answered on 30 Sep 2014, 08:18 AM
Hello Pat,

I tried to replicate the issue locally by using the following live example but to no avail. I attached the modified sample to this tread, so could you please give it a try and let me know about the result. I would appreciate if you could replicate the issue in my sample or provide a small runnable one in order to investigate it further.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jason Sheats
Top achievements
Rank 1
Answers by
Johny
Top achievements
Rank 1
Pat
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or