Re: ASP and Forms

by "rudy" <r937(at)interlog.com>

 Date:  Mon, 29 Jan 2001 22:23:43 -0500
 To:  <hwglister(at)mail.technicalcommunication.com>,
<hwg-techniques(at)hwg.org>
  todo: View Thread, Original
> I have a form where I'm saving all the inputs into an SQL db.
> What I want to do is to be able to follow the SAVE with a display
> page that shows the saved info. The problem I'm having is that until
> the story is saved, it doesn't have an 'ID' in the db just yet, so what
> would be the best way to make the info display on the display page?
>
> The display page, by the way, is used by several different scripts,
> which is why I'm using 'storyID' as the governing field.
>
> Hope this makes sense, and thanks in advance!


hi imran

yes, makes perfect sense

a lot depends on which database you're using, and whether the storyID field
is automatically generated (e.g. sequence field in oracle, identity field
in sql/server)

there are tricks with asp that can return the number that was assigned as
part of the insert command, but the tricks are ugly and i don't know them
;o)

in most databases you will have to perform two steps -- either go get the
next number for storyID (possible in oracle, not possible in sql/server)
and then use it on both the subsequent insert and the display, or else
insert the record, and then run a query to get back the inserted row along
with the storyID

if you do the latter (insert first and then query to get it back), the
trick is to have some combination of fields that will be defined unique

do *not* assume, if you are using a sequence or identity field, that you
can just do a SELECT MAX() and still get the same number you just
inserted -- not unless you wrap both in a transaction block

anyhow, in simple terms, here's what you need

in your case, story title might be sufficiently unique, or perhaps story
title and author

you have to declare a unique index on the unique field(s) -- this is not
your primary key index, your primary key is storyID, this is a separate and
additional index

you want to choose it in such a way that you will never get somebody
attempting to insert a duplicate row based on the unique index -- for
example, the same story title *and* author

after you do your insert, just run a query where you select the row using
the unique index field(s)

voil�


rudy.ca

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA