There is a very good reason for this.
On the save of a new record the saved record is now reloaded in the odata object of the data object. The page is now no longer
in new record mode and is now in edit mode. The new primary key of the record gets loaded into the page when the page reposts back to itself.
If a user is adding a new record on the page, they may remain there and add more info. Or maybe they added a header/master record and child records all on
the same page. Before saving the child records you need the key of the master record.
Also, a database trigger may update data that is not specifically added to a record on a save of a new record.
So while not strictly necessary in every case, I find it very useful to reload the record after saving so that the web page code (OnRender etc) always knows that the oData property
has a record.
When I am adding a new record, I also do a load of the oData member (Key = 0) because I find having an empty record useful on the page.
As for the parameter on the querystring (generally identifying that a new record is required) I only look at that on the initial load of the page (setting a preserved page property)
which tells me whether I am adding a new record or editing an existing one.
After a valid save, the property is changed. If the record can't be saved because of a validation problem, the page stays in New mode.
I don't think you need to use session for this. By default, all my web pages (subclassed webpage) have an nprimaykey property for the master record of the page.
This is a preserved property so once a new record is added, I know the key of the master record and on an edit, it is automatically loaded as well.
Bob
Bob,
Thanks. I was not sure if Rick had an ADO call to update an identity column on an insert, but @@IDENTITY was my back-up plan. I have used @@IDENTITY going back to SQL 6.5 and I am only adding one row at a time.
Question: Why do you reload the new row, after getting the identity value? Is that to return any default values? I am just wondering because it does not seem like it will help me as far as the identity column goes, since it is a stateless app.
Since Web apps are stateless, the next button click will have to reload the row. However, I want to make sure that I don't reload a row or insert a new row, based on a URL parameter. I was thinking of using a session variable to store the identity value and another session variable to store the operation. So, I add a contact. Now, I want to add an order. When I click the button to add the order, look at the session variable, rather than the URL parameter.
Thanks,
David
Hi David;
I have been using SQL Server with a number of applications.
I have subclassed the wwsql class and modified a few of the methods.
Specifically saving data (I have columns that can have default null values, or have a default value like GETDATE() and so my code specifically will not include those fields
on an insert if the value is null. Also, getting back the primary key is valuable too!)
Anyway, here is the sub-classed code of my save method.
Note that after the save of data, I do a specific call to the load method with the primary key to
load the oData object with the values just saved, including the primary key.
The buildrecordinfo is an extra method I added for cases where I wanted to display the date and time of the last edit
(pretty well all my tables have columns for who created the record, when it was created, who modified and when modified.
These date fields are updated by a triggers)
I should be using Scope_Identity() instead of @@Identity but as long as inserts don't add records in other tables I am okay.
But I should update this. (This code has been in place for years!)
Bob
Hi,
I am going to use a wwBusiness subclass to connect to an MS SQL table, which has an identity (auto increment) primary key column. I am using wwSQL to connect to the SQL server. I assume that I have to specify the PK column for cSkipFieldsForUpdates. When I insert a new row, the PK column will not have a value. When I save the row, will the wwBusiness class refresh the identity value or do I have to make a call to @@IDENTITY?
Thanks,
David