Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 3rd, 2003, 05:55 PM
Authorized User
 
Join Date: Jun 2003
Location: Sylmar, CA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default passing variable

i am using SQL as db.
i need to select the id from the row i just created, then pass that id to another page.
how do i do this?

i heard that easiest way is using a stored procedure. but i can't create stored procedure. is that any way to do that?
Reply With Quote
  #2 (permalink)  
Old June 3rd, 2003, 06:07 PM
Authorized User
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps you could give us a little more background on your problem, what are you using your DB with? ASP I assume as your posting in this forum.

I guess the basics would go like this.

Create a page that...

A. Selects the new ID from the new row in your table
B. Selects from your other table using a WHERE statement with the old ID

I hope this helps!

Al.
Reply With Quote
  #3 (permalink)  
Old June 3rd, 2003, 06:48 PM
Registered User
 
Join Date: Jun 2003
Location: Omaha, Nebraska, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is in "old ASP" format, I don't know ASPX yet, so if you are using that, someone else will have to answer...

example1.asp
Code:
<% Response.Buffer = True %>
<%
'This one will add a new row, obtain the new row ID, and pass it to the example2.asp
'
'Table contains three elements: ID, SomeInfo, MoreInfo
'ID is an autoincrement type

Set rsMessage    = CreateObject("ADODB.Recordset")
rsMessage.Open "Message", "Messages", adOpenKeySet, adLockPessimistic, adCmdTable
rsMessage.AddNew

rsMessage("SomeInfo")  = InfoA
rsMessage("MoreInfo")  = InfoB

rsMessage.Update

'rsMessage.Update will return value of new row in rsMessage.ID

NewRowID = rsMessage("ID")

'Store the row ID in the session object. You could just pass this in QueryString, but
'not good security - could be exploited since it is passed to the client.
Session("NewRowID") = NewRowID

rsMessage.Close

Response.Redirect("Example2.asp")

%>

example2.asp
Code:
<%
'Retrieve value from Session object (Not neccessarily needed, but makes for cleaner code)
RowID = Session("NewRowID")

'Display passed Row ID
Response.Write("New Row ID = " & RowID & "<br>")

'You now have the Row ID in RowID and can do whatever you want with it...
%>
Reply With Quote
  #4 (permalink)  
Old June 6th, 2003, 06:25 PM
Authorized User
 
Join Date: Jun 2003
Location: Sylmar, CA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default

i am building registration over the web. but the survey is too long i can't put it in one age.
so what i do is to insert the data from one page, and other pages just update the row. so too keep the same primary key, i need to pass the primary key to next page.

i do not know how to get the primary key after inserting the data. and then i need some way to pass it to the next page.
Reply With Quote
  #5 (permalink)  
Old June 6th, 2003, 07:32 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Assuming your using SQL Server, you would use a stored procedure that returns the @@Identity.

The other option is to make a surrogate key that is of type UniqueIdentifier (which is a guid) and then in your ASP page use guid.new to generate a new guid.

Personally, I prefer to generate guid's myself then rely on an autonumber and an @@Identity return- but many people use @Identity successfully.

Hal Levy
Daddyshome, LLC
Reply With Quote
  #6 (permalink)  
Old June 6th, 2003, 07:46 PM
Authorized User
 
Join Date: Jun 2003
Location: Sylmar, CA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default

Thank you Hal Levy.;)

Yes i am using SQL server and asp.net.


i tried to use @@identity. and i was able to get the primary key.
however, i still have the problem of sending it to the next page.
Reply With Quote
  #7 (permalink)  
Old June 7th, 2003, 11:53 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You submit the form.. The form does it's work.. After the form has finished it's work there are a few options (There are more than 3.. I am just giving you 3 of them)

1. Set the identity to a Cookie on the users macine
2. Put the identity in the session
3. On the redirect to the second form after the first is done- append the ID to the URL and then retrieve it from the URL on the second page.

Hal Levy
Daddyshome, LLC
Reply With Quote
  #8 (permalink)  
Old June 10th, 2003, 08:08 PM
Authorized User
 
Join Date: Jun 2003
Location: Sylmar, CA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to gumgak
Default

Thanks Hal Levy. :D

but i am still having the problem
below is my source code

1st page===============

    "SELECT @@IDENTITY AS 'Identity';";

// Create the Command and set its properties
SqlCommand objCmd = new SqlCommand(strSQL, objConnection);

// save Identity in a cookie
HttpCookie RegCookie = new HttpCookie("NewId");
RegCookie.Value = Convert.ToString(Identity);
Response.Cookies.Add(RegCookie);



second page============================

int GetRegID()
{
    String regID = Request.Cookies["NewID"].Value;
    return Convert.ToInt16(regID);
}


=========================================

the second page says that the id is 0. obviously, the first page is not sending the correct ID.
Reply With Quote
  #9 (permalink)  
Old June 11th, 2003, 03:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey, .NET isn't my thing, but surely you need some object which enables you to get at the row returned by the SQL statement - e.g. a Recordset object in classic ADO. I'm sure someone else can tell you what the .NET equivalent is, or maybe you could look it up in MSDN...
Reply With Quote
  #10 (permalink)  
Old June 11th, 2003, 07:41 PM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,102
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Umm.. your code never sets Identity.. Is that your ACTUAL code or cuts and pastes from it?
If that's your code... it won't work. Let me see the full code.

Hal Levy
Daddyshome, LLC
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a Variable LarryS Beginning PHP 1 August 8th, 2006 07:52 AM
passing variable tbekarov ASP.NET 2.0 Professional 7 February 1st, 2006 03:16 PM
Passing variable to a public variable Hudson40 Access VBA 2 February 25th, 2005 05:23 AM
Passing a variable mrideout BOOK: Beginning ASP.NET 1.0 3 August 6th, 2004 02:57 PM
Passing a variable trekmp ADO.NET 1 March 4th, 2004 12:57 PM



All times are GMT -4. The time now is 12:33 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.