Classic ASP DatabasesDiscuss 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 .
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...
%>
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.
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.
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.
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.
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...
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.