Advertisement

03.27.2008 at 04:19AM PDT, ID: 23273544
[x]
Attachment Details

insert new record, get last insert id and rename file

Asked by ellandrd in Active Server Pages (ASP)

I want to insert a new record into my table using form data.  I also upload a file to a folder too.  I then get the last insert ID value and using that last insert ID value, i want to rename the uploaded file from <filename>.<extension> to <last_insert_id>.<extension>

see my code below.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
<%@ Language="VBScript" %>
<% Option Explicit %>
<% Response.CacheControl = "no-cache" %>
<% Response.AddHeader "Pragma", "no-cache" %>
<% Response.Expires = -1 %>
<% Server.ScriptTimeout = 600 %>
<!--#include file="upload.asp"-->
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 
	Dim uploadsDirVar, Upload, dbRecordSet, dbQuery, dbInsertStatement
        Dim dbSelectStatement, dbConnection, strFullname, strComments, strDateTime
	Dim strFile
	Dim strLastID
	Dim strNewFileName
	
	uploadsDirVar = Server.MapPath("cvs")
	
	Set Upload = New FreeASPUpload
    
	Upload.Save(uploadsDirVar)
 
	strFullname = Upload.Form("fullname")
	strComments = Upload.Form("comments")
	strDateTime = Upload.Form("datetime")
 
	'For Each strFile In Upload.UploadedFiles.Items
	'	Response.Write("Name: " & strFile.FileName & "<br>")
	'	Response.Write("Type: " & strFile.ContentType & "<br>")
	'	Response.Write("Start: " & strFile.Start & "<br>")
	'	Response.Write("Size: " & strFile.Length & "<br>")
	'Next
 
	dbQuery = "DRIVER={SQL Server};SERVER=FILE04ABZPI;DATABASE=CVManager;UID=sa;PASSWORD=;" 
 
	Set dbConnection = Server.CreateObject("ADODB.Connection") 
	dbConnection.Open(dbQuery) 
 
	dbInsertStatement = "INSERT INTO tblCandidates (Fullname,Comments,DateTime) VALUES ('" & strFullname & "','" & strComments & "','" & strDateTime & "')"
 
	Response.Write(dbInsertStatement)
	
	dbConnection.Execute(dbInsertStatement)
	
	dbSelectStatement = "SELECT IDENT_CURRENT('tblCandidates') AS LastID"
	Set dbRecordSet = dbConnection.Execute(dbSelectStatement)
 
	Do While Not dbRecordSet.EOF
		Set strLastID = dbRecordSet("LastID")
		dbRecordSet.MoveNext
	Loop
 
	dbConnection.Close
 
	'Response.Write("Last Insert ID: " & strLastID)
	
	'Response.End
 
	Set objFSO = CreateObject("Scripting.FileSystemObject")
 
    MoveFrom = Server.MapPath("cvs/" & Upload.UploadedFiles.Items.FileName)
    MoveTo = Server.MapPath("cvs/" & strLastID)
    objFSO.MoveFile MoveFrom, MoveTo
	
	Set objFSO = Nothing
	Set dbRecordSet = Nothing
	Set dbConnection = Nothing
 
End If
%>
[+][-]03.27.2008 at 05:58AM PDT, ID: 21220767

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.27.2008 at 06:01AM PDT, ID: 21220793

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Active Server Pages (ASP)
Sign Up Now!
Solution Provided By: ellandrd
Participating Experts: 2
Solution Grade: B
 
 
[+][-]03.27.2008 at 06:06AM PDT, ID: 21220840

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.29.2008 at 06:58PM PDT, ID: 21239175

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906