iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
With WebGrid 8, just after inserting a new row, if I try to edit it, when the update code is supposed to run, I get :
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
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.]
ISNet.WebUI.WebGrid.WebGridTable.IsKeyValueEqual(WebGridRow row, Object oKeyValues) +149
..(DataView , XmlNode ) +2559
..(DataView , String ) +1200
..(DataView , String , String ) +1466
..DataBind(Object , String ) +1584
ISNet.WebUI.WebGrid.WebGrid.() +183
ISNet.WebUI.ISNetControl.ControlRequestHandler(Object , EventArgs ) +303
[Exception: Object reference not set to an instance of an object.]
ISNet.WebUI.ISNetControl.ControlRequestHandler(Object , EventArgs ) +578
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnPreRender(EventArgs e) +92
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
In the AddRow handler, I insert the new line in SQL Server. The Key is autogenerated by SQL Server.
Then I read this new Key and set in the Value of the corresponding WebGridCell in the new row.
Is there something else I need to do to correctly set the row's DataKeyField?
Thank you,
Could you please try to invoke WebGrid1.ClientAction.Refresh() after insert the new line in SQL Server? In the meantime, I’ll try to get a sample for you to be evaluated.
Hope this helps.
I’m quite sure that you forgot to set the DataKeyField property in RootTable.
For example:
<ISWebGrid:WebGrid ID="WebGrid1" runat="server" ...> <RootTable Caption="Customers" DataKeyField="CustomerID" DataMember="Customers"> ... </RootTable> <LayoutSettings AllowEdit="Yes"> </LayoutSettings> </ISWebGrid:WebGrid>
Hi, thank you for the reply.
But no, it is there:
<iswebgrid:WebGrid ID="WebGrid1" runat="server" DataMember="PAP_CAR_SCHEDULE" Width="776px" RenderingMode="HTML5" UseDefaultStyle="True"> <RootTable DataKeyField="CAR_SCHEDULE_ID" CaptionImage="order-details.gif" TableHeaderVis ible="True" Caption="Car Schedule" DataMember="PAP_CAR_SCHEDULE">
... </RootTable> ... </iswebgrid:WebGrid>
What I forgot to mention is that if I reload the page, the added row is there and can be edited just fine without giving errors. It happens only with rows that have just been added.
I’d like to confirm whether you have executed SELECT @@identity after the insert command. It will allow you to save off the identity that was inserted.
In fact it is a 3rd party object that does the database commands.
Here is the VB.NET code:
Private Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs) Handles WebGrid1.AddRow Dim cells As New ISNet.WebUI.WebGrid.WebGridCellCollection cells = e.Row.Cells If cells.Item(0).Text <> "" Then UpdateSchedule(cells) End If End Sub
Private Sub UpdateSchedule(ByVal cells As ISNet.WebUI.WebGrid.WebGridCellCollection) Dim rsSchedule As TxAppFramework.Core.Metabase = New TxAppFramework.Core.Metabase(1000000110, TxSession) rsSchedule.Data.Open() rsSchedule.Data.AddNew() rsSchedule.Data.Value("SERVICEMEMORANDUM_ID") = Context.Request("RecordID") rsSchedule.Data.Value("TIME") = cells.Item(0).Text rsSchedule.Data.Value("V_PAP_CAR_TYPE_ID") = cells.Item(1).Value rsSchedule.Data.Value("ADDRESS") = UCase(cells.Item(2).Text) rsSchedule.Data.Value("DRIVER") = UCase(cells.Item(3).Text) rsSchedule.Data.Value("V_PAP_SCHED_TYPE_ID") = cells.Item(4).Value rsSchedule.Data.Value("NO_CARS") = cells.Item(5).Text rsSchedule.Data.Value("NOTES") = cells.Item(6).Text) rsSchedule.Data.Update() 'This is where the ID is read and set in the Grid cell cells.Item(8).Value = CStr(rsSchedule.Data.ID()) cells.Item(8).Text = CStr(rsSchedule.Data.ID()) rsSchedule.Data.Close() rsSchedule = Nothing End Sub
So the new identity is returned by the "rsSchedule" object,
and is set as the value and text of the cell corresponding to the column that has the key field (index #8). Is there somewhere else it should be set?
When a new row is inserted through database adapter compatible provider, it does not return the new row’s identity by default. Thus, developers are required to write additional codes to programmatically obtain the new row’s identity.
Following C# code show how to retrieve new identity for ObjectDataSource control.
[DataObjectMethodAttribute(DataObjectMethodType.Insert, true)] public int DoInsert(ref Nullable OrderID, string CustomerID, Nullable EmployeeID, Nullable OrderDate, Nullable RequiredDate, Nullable ShippedDate, Nullable ShipVia, Nullable Freight, string ShipName, string ShipAddress, string ShipCity, string ShipRegion, string ShipPostalCode, string ShipCountry) { this.Connection.Open(); // important int affectedRows = this.Insert(CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry); int identity = this.SelectIdentityQuery(); OrderID = identity; // pass the new identity into OrderID param this.Connection.Close(); return affectedRows; } public int SelectIdentityQuery() { OleDbCommandcmd = new OleDbCommand("SELECT @@IDENTITY", this.Connection); int newID = (int)cmd.ExecuteScalar(); return newID; }
I’m not sure what function/method which equals to @@IDENTITY in your 3rd party object.
It is the .Data.ID() method rsSchedule.Data.ID()
But it might be easier for me to understand if we start from your example.
Is your DoInsert method called from the Webgrid AddRow event? If so, after the method is called, what do you do with the returned OrderID parameter? How is it assigned to the webgrid?
Ok, no need for the example, calling WebGrid1.ClientAction.Refresh() solved the problem.
Thank you very much!
Glad to hear that invoking Refresh() solve the problem.
Should you need further assistance or run into any problems regarding our controls, feel free to post it into our forum. We would be happy to assist you again.
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname