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

Insert new item throws object reference error

3 Answers 50 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Venkatesh
Top achievements
Rank 1
Venkatesh asked on 04 Dec 2012, 07:08 AM
Hi,
I am using RAD Grid in sharepoint 2010. I have added the insercommand and when i click on save in the grid, i am getting "object reference not set to instance of object error".
Below is the code i have used:
<div>
         
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
<telerik:RadGrid ID="RadGrid1" runat="server" EnableViewState="true" ShowStatusBar="true"
        ShowFooter="True" onitemcommand="RadGrid1_ItemCommand" 
        ondeletecommand="RadGrid1_DeleteCommand" 
        oninsertcommand="RadGrid1_InsertCommand" 
        onupdatecommand="RadGrid1_UpdateCommand" 
        onneeddatasource="RadGrid1_NeedDataSource" >
    <MasterTableView DataKeyNames="Title" AutoGenerateColumns="false" EditMode="InPlace" CommandItemDisplay="TopAndBottom" CommandItemSettings-AddNewRecordText="Add New Item" >
                    <Columns>
                     <telerik:GridEditCommandColumn ButtonType="ImageButton">
                        </telerik:GridEditCommandColumn>
                        <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
                            ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" ConfirmDialogHeight="100px"
                            ConfirmDialogWidth="220px">
                        </telerik:GridButtonColumn>
  
                     <telerik:GridTemplateColumn DataField="Title" HeaderText="Product Number" UniqueName="ProductNumber"
                            Visible="true">
                            <InsertItemTemplate>
                                <telerik:RadTextBox ID="RadtxtPrdNumber" runat="server" Text='<%# Bind("Title") %>' >
                                </telerik:RadTextBox>
                            </InsertItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadTextBox ID="RadtxtPrdNumber" runat="server" Text='<%# Eval("Title") %>' >
                                </telerik:RadTextBox>
                            </EditItemTemplate>    
                            <ItemTemplate>
                            <telerik:RadTextBox ID="RadtxtPrdNumber" ReadOnly="true" runat="server" Text='<%# Eval("Title") %>' />
                            </ItemTemplate>                       
                        </telerik:GridTemplateColumn>
  
 </Columns>
      
    </MasterTableView>
     <ClientSettings EnableRowHoverStyle="true">
        </ClientSettings>
  
    </telerik:RadGrid>
  
  
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
        onclick="btnSubmit_Click" />
</div>

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            Hashtable values = new Hashtable();
            item.ExtractValues(values);
            string ti = values["Title"].ToString();
}

In the Insercommand code, values is coming as null.
How to fix this?
Thanks

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Dec 2012, 07:20 AM
Hi,

I suppose you want to access the new values in RadTextBox. Here is the sample code.
C#:
void RadGrid2_InsertCommand(object sender, GridCommandEventArgs e)
{
        GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
        RadTextBox txt = (RadTextBox)item.FindControl("RadtxtPrdNumber");
           string value = txt.Text;

}

Thanks,
Shinu.
0
Venkatesh
Top achievements
Rank 1
answered on 04 Dec 2012, 08:13 AM
Hi,
I changed the code as you mentioned. I am getting the error as below:
"Unable to cast object of type 'Telerik.Web.UI.GridDataInsertItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'."
Also, when i commented the below AJAX code, my earlier code is working and i am getting the values in InsertCommand. What needs to be changed in the below code.
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2012, 07:55 AM
Hi,

I suppose you are using InPlace edit mode. Try accessing the textbox as shown below.
C#:
void RadGrid2_InsertCommand(object sender, GridCommandEventArgs e)
{
        GridDataInsertItem item = (GridDataInsertItem)e.Item;
        RadTextBox txt = (RadTextBox)item.FindControl("RadtxtPrdNumber");
        string value = txt.Text;
}

Thanks,
Shinu.
Tags
Grid
Asked by
Venkatesh
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Venkatesh
Top achievements
Rank 1
Share this question
or