Solved

How to retrieve Unique ID after record insertion / dreamweaver cs6 / Micrsoft SQL server

Posted on 2013-01-04
5
878 Views
Last Modified: 2013-01-09
I am adding a page using .asp and dreamweaver cs6.  the first page loads information and the second page will be a photo upload page, but must know the identity of the record just inserted.  I used to have an extension that did this (Tom Mucks) that would load the Id into a session variable.  That extension doesn't work now in Dreamweaver CS6.  I'm looking for the best way to do this.  I know its with @@identity, but not really sure how to use it?
0
Comment
Question by:Baub Eis
5 Comments
 
LVL 42

Accepted Solution

by:
EugeneZ earned 200 total points
ID: 38746095
it depends to what you need:

there are several options
SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions in that they return values inserted into IDENTITY columns.

IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT.

SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.


try to use
SCOPE_IDENTITY
more with code examples (@@identity as well)
http://msdn.microsoft.com/en-us/library/aa259185(SQL.80).aspx
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 150 total points
ID: 38746506
hi

select scope_identity() -- your specific sessions insert

select @@identity -- the last identity used
0
 

Author Comment

by:Baub Eis
ID: 38752800
Ok I have two pages.

1st page adds basic info
2nd page adds photo

Do I use the scope_identity() on the first page and put into session variable?  Or do I put it on the second page in the SQL statement?
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 150 total points
ID: 38757267
First page and pass it to the second page using either a session variable or some other mechanism.
0
 

Author Comment

by:Baub Eis
ID: 38758922
ok I think I can pass it to the second page with a session variable, but not sure how to implement it into the default dreamweaver code.  I didn't used to have a problem, but then they rewrote they way they handle querys etc due to SQL injection attacks.  Here is just a blank page that inserts into one column:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/durgorama.asp" -->
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(Request("MM_insert")) = "form1") Then
  If (Not MM_abortEdit) Then
    ' execute the insert
    Dim MM_editCmd

    Set MM_editCmd = Server.CreateObject ("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_durgorama_STRING
    MM_editCmd.CommandText = "INSERT INTO dbo.baubscope (scopebaub) VALUES (?)" 
    MM_editCmd.Prepared = true
    MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 50, Request.Form("scopebaub")) ' adVarWChar
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    ' append the query string to the redirect URL
    Dim MM_editRedirectUrl
    MM_editRedirectUrl = "scopetest2.asp"
    If (Request.QueryString <> "") Then
      If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0) Then
        MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
      Else
        MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
      End If
    End If
    Response.Redirect(MM_editRedirectUrl)
  End If
End If
%>
<%
Dim baubscope
Dim baubscope_cmd
Dim baubscope_numRows

Set baubscope_cmd = Server.CreateObject ("ADODB.Command")
baubscope_cmd.ActiveConnection = MM_durgorama_STRING
baubscope_cmd.CommandText = "SELECT * FROM dbo.baubscope" 
baubscope_cmd.Prepared = true

Set baubscope = baubscope_cmd.Execute
baubscope_numRows = 0
%>
<!doctype html>
<html><!-- InstanceBegin template="/Templates/DetailPage16grid.dwt.asp" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="UTF-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>GlobalBizBuySell</title>
<!-- InstanceEndEditable -->
<link rel="stylesheet" type="text/css" href="Styles/960grid/960_16_col.css" />
<link rel="stylesheet" type="text/css" href="Styles/960grid/reset.css" />
<link rel="stylesheet" type="text/css" href="Styles/960grid/text.css" />
<link href="Styles/960grid/baub.css" rel="stylesheet" type="text/css">
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div id="header" class="container_16 clearfix">
  <div class="grid_16"><img src="images/top_header_temp.jpg" width="940" height="50" usemap="#Map">
    <map name="Map">
      <area shape="rect" coords="526,29,574,48" href="default2.asp">
      <area shape="rect" coords="886,29,934,50" href="admin.asp">
      <area shape="rect" coords="581,29,704,48" href="buyabusiness.asp">
      <area shape="rect" coords="712,28,839,47" href="Sellabusiness.asp">
    </map>
  <img src="images/bottom_footer.jpg" width="940" height="45">  </div>
</div>
<div id="content" class="container_16 clearfix">
  <div class="grid_12"><!-- InstanceBeginEditable name="MainContent" -->
    <form name="form1" method="POST" action="<%=MM_editAction%>">
      <input type="text" name="scopebaub" id="scopebaub">
      <input type="submit" name="button" id="button" value="Submit">
      <input type="hidden" name="MM_insert" value="form1">
    </form>
  <!-- InstanceEndEditable --></div>
  <div class="grid_4"><!-- InstanceBeginEditable name="SidebarContent" -->SidebarContent<!-- InstanceEndEditable --></div>
</div>
<div id="footer" class="container_16 clearfix">
  <div class="grid_16"><img src="images/bottom_footer.jpg" width="940" height="45"><br>
    GLOBALBIZBUYSELL.COM | (636) 
 &nbsp;497 - 5597 </div>
</div>
</body>
<!-- InstanceEnd --></html>
<%
baubscope.Close()
Set baubscope = Nothing
%>

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now