Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Professional
Classic ASP Professional For advanced coder questions in ASP 3. 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 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 16th, 2012, 08:06 AM
Registered User
Join Date: Jan 2012
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default save file in db with unique name or id with asp classic

I m new to asp my code is working absolutely fine, but i am facing two problems although these problems are not affecting the application. I just want to make good functionality to the application logically.

1) when i upload excel file, and if another file is already present with the same name, then new file overwrites it..
and if i manually change file name then file is saved with new name..
i am trying to save this new file with unique name or new name,, i don’t know how to do this pls help..

2) and second problem is that i want to save this file in db with the corresponding login user (for future reference).. i hope
my problems will be solved. many thanks

this is the url from which I got help..

code is pasted for review...

<form action="upload_excel_process.asp" method="post" enctype="multipart/form-data" name="frmMain" onSubmit="return checkData();">
<input name="file1" type="file"> <input type="submit" name="Submit" value="Submit">
<input type="hidden" name="client_id"  value="<%=mem_id%>">
process file

Dim xlApp,xlBook,xlSheet1,xlSheet2,OpenFile,i  
Dim Conn,strSQL,client_id,objExec  
Dim mySmartUpload  
Dim sFileName	

'*** Create Object ***'	
Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")

'*** Upload Files ***'

	'** Getfile Name ***'
sFileName = mySmartUpload.Files("file1").FileName

If sFileName <> "" Then


'   my ref..\excel\" & session("mem_id") & "\" & now()

OpenFile = "excel/"&sFileName

'*** Create Exce.Application ***'
Set xlApp = Server.CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
Set xlSheet1 = xlBook.Worksheets(1)		

Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("../db/database.mdb"),"" , ""
For  i = 2 To 5  
If Trim(xlSheet1.Cells.Item(i,1)) <> "" Then  
strSQL = ""  
strSQL = strSQL &"INSERT INTO add_contacts "  
strSQL = strSQL &"(client_id,name_receiver,contact_person_receiver,street_receiver,city_receiver,tel_receiver,fax_receiver,country_receiver,zip_code_receiver) "  

' i have added fields to the db for file (file_name and file_id) 

strSQL = strSQL &"VALUES "  
strSQL = strSQL &"('"&client_id&"', '"&xlSheet1.Cells.Item(i,1)&"','"&xlSheet1.Cells.Item(i,2)&"','"&xlSheet1.Cells.Item(i,3)&"'"  
strSQL = strSQL &",'"&xlSheet1.Cells.Item(i,4)&"','"&xlSheet1.Cells.Item(i,5)&"','"&xlSheet1.Cells.Item(i,6)&"','"&xlSheet1.Cells.Item(i,7)&"','"&xlSheet1.Cells.Item(i,8)&"')"

Set objExec = Conn.Execute(strSQL)  
Set objExec = Nothing  
End IF  


'*** Quit and Clear Object ***'
Set Conn = Nothing
Set xlSheet1 = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End If

Set mySmartUpload = Nothing
Old January 16th, 2012, 06:09 PM
Friend of Wrox
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41

simply, you need to create a unique string and use this for the filename. I use upload code that provides a function that will create a GUID. However here is a very quick, very simple way to apply a unique name which you can use for a file name:

Dim nRandom, theFileName
nRandom = (100-1)*Rnd+1
theFileName =monthname(month(date)) &  cstr(year(date)) & "_" & CStr(nRandom) & ".xls"

The output, as you can see will be MonthNameYearXXXXX (XXXX being the random unique part).

Your second issue is also very simple. You need to save what ever the unique identifyer is for the user to a new filed in the table where you are storing the fileName. I assign the ID field (which is an auto incrementing primary key) in my user table when the user logs in to a page level variable. Then this variable is passed from page to page and is inserted into the DB where ever applicable.
Wind is your friend

Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel to Access DB Classic ASP Unique Records asipoy Classic ASP Databases 0 January 12th, 2012 04:21 AM
ASP with MS Access unique ID AA00 -> ZZ99 sv3n1980 Access ASP 0 May 9th, 2007 08:11 AM
free web hosting for Classic ASP and a MySQL DB crmpicco Classic ASP Basics 1 March 14th, 2007 06:00 AM
accessing MS SQL DB from Classic ASP script crmpicco SQL Server ASP 2 June 1st, 2006 03:43 PM
create a unique id in asp code langer123 Classic ASP Basics 1 March 15th, 2005 03:55 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.