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

Object reference not set to an instance of an object in OnPreRender after setting .Edit and rebinding in ItemCommand

1 Answer 154 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 10 Nov 2008, 05:41 PM
Hello - I've got a pretty complex RadGrid mostly working, except for one case.  In this case, when the user clicks a button, I change a hidden field, rebind the grid (which produces a slightly different recordset, adding one row), and I want to select that particular row and put it in edit mode.

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            switch (e.CommandName) 
            { 
                case "New": hdnForceBlankShiftForEmployeeID.Value = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"].ToString(); 
                    RadGrid1.Rebind(); 
                    Hashtable dictionary = new Hashtable(2); 
                    dictionary.Add("EmployeeID",Convert.ToInt32(hdnForceBlankShiftForEmployeeID.Value)); 
                    dictionary.Add("EmployeeShiftID",System.DBNull.Value); 
                    GridDataItem[] myArray = RadGrid1.MasterTableView.FindItemsByKeyValues(dictionary); 
                    if (myArray.Length> 0) 
                    { 
                        myArray[0].Edit = true
                    } 
                    break
            } 
        } 

However, when I do this, I get the error below.  It's coming from setting the .Edit = true, as if I comment that out, it all works (other than the record not being in edit mode, of course).  Oh, incidentally, I don't have an onprerender - it's just whatever the base functionality is.

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about the error andwhere it originated in the code.

Exception Details:System.NullReferenceException: Object reference not set to an instance of anobject.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]   Telerik.Web.UI.RadGrid.OnPreRender(EventArgs e) +1302   System.Web.UI.Control.PreRenderRecursiveInternal() +80   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Control.PreRenderRecursiveInternal() +171   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

So I'm assuming my approach is wrong somehow.  It's a complicated page, so I'm hoping this fragment is enough to point out my mistake, but if not, let me know...


Thanks,

Scott

1 Answer, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 10 Nov 2008, 06:17 PM
GAH!!!!!!  See, I step away for 30 minutes, come back, and re-read the docs for GridDataItem.Edit, which clearly state:

Sets the Item in edit mode. Requires Telerik RadGrid to rebind.

I think I got distracted because I was already rebinding once. So, if I adjust the code to the below (adding a second rebind), it works. 
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            switch (e.CommandName) 
            { 
                case "New": hdnForceBlankShiftForEmployeeID.Value = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"].ToString(); 
                    RadGrid1.Rebind(); 
                    Hashtable dictionary = new Hashtable(2); 
                    dictionary.Add("EmployeeID",Convert.ToInt32(hdnForceBlankShiftForEmployeeID.Value)); 
                    dictionary.Add("EmployeeShiftID",System.DBNull.Value); 
                    GridDataItem[] myArray = RadGrid1.MasterTableView.FindItemsByKeyValues(dictionary); 
                    if (myArray.Length> 0) 
                    { 
                        myArray[0].Edit = true
                    } 
                    RadGrid1.Rebind(); 
                    break
            } 
        } 

Not incredibly efficient, so i'm open to a better way if anyone's got one... 
Scott
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Share this question
or