Link to home
Start Free TrialLog in
Avatar of FSAAdmin
FSAAdminFlag for United States of America

asked on

Using FreeASPUpload & Inserting Record at same time

I had previously posted a question on how to upload a file and insert a record (2 actions with one button) https://www.experts-exchange.com/questions/26465103/Upload-File-and-Insert-Record-into-Database-2-Actions-with-1-Button.html.  
I thought a more appropriate title would help in getting this resolved since I'm using FreeASPUpload to upload the file.

I came across the FreeASPUpload.  Before using FreeASPUpload, I was able to save a record to the database with the file name store in a field called "Attachment".    

Since I want the users to be able to retrieve the attachment later, I also need to use FreeASPUpload to upload the file.  I'm at the point now where I can upload the file but I'm getting an error when trying to insert the record.

I've read through a handful of questions and solutions on this forum where others have been trying to do the same thing.  However I am unable to determine the clear solution.

I am now getting an error on line 93 saying that it could not find the file / database.  I have given full  permissions to the guest account on the folder where the database resides.  

So now it seems that i need help getting the insert statement to work.
<%@ Language=VBScript %>
<% 
option explicit 
Response.Expires = -1
Server.ScriptTimeout = 600
' All communication must be in UTF-8, including the response back from the request
Session.CodePage  = 65001
%>
<!-- #include file="freeaspupload.asp" -->

<%


  ' ****************************************************
  ' Change the value of the variable below to the pathname
  ' of a directory with write permissions, for example "C:\Inetpub\wwwroot"
  ' ****************************************************

  Dim uploadsDirVar
  uploadsDirVar = "c:\inetpub\wwwroot\FacilityConditionTracking\files" 
  

  ' Note: this file uploadTester.asp is just an example to demonstrate
  ' the capabilities of the freeASPUpload.asp class. There are no plans
  ' to add any new features to uploadTester.asp itself. Feel free to add
  ' your own code. If you are building a content management system, you
  ' may also want to consider this script: http://www.webfilebrowser.com/

function OutputForm()
%>
    <form name="frmSend" method="POST" enctype="multipart/form-data" accept-charset="utf-8" action="uploadTester4.asp" onSubmit="return onSubmitForm();">
	<B>File names:</B><br>
    File 1: <input name="attach1" type="file" size=35><br>
    <br> 
	<!-- These input elements are obviously optional and just included here for demonstration purposes -->
	<B>Additional fields (demo):</B><br>
	Enter an AiM Work Order Number: <input type="text" name="AimWO"><br>
    <input style="margin-top:4" type=submit value="Upload">
    </form>
<%
end function

function TestEnvironment()
    Dim fso, fileName, testFile, streamTest
    TestEnvironment = ""
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    if not fso.FolderExists(uploadsDirVar) then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
        exit function
    end if
    fileName = uploadsDirVar & "\test.txt"
    on error resume next
    Set testFile = fso.CreateTextFile(fileName, true)
    If Err.Number<>0 then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
        exit function
    end if
    Err.Clear
    testFile.Close
    fso.DeleteFile(fileName)
    If Err.Number<>0 then
        TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder."
        exit function
    end if
    Err.Clear
    Set streamTest = Server.CreateObject("ADODB.Stream")
    If Err.Number<>0 then
        TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries."
        exit function
    end if
    Set streamTest = Nothing
end function

function SaveFiles
    Dim Upload, fileName, fileSize, ks, i, fileKey

    Set Upload = New FreeASPUpload
    Upload.Save(uploadsDirVar)

    ' If something fails inside the script, but the exception is handled
    If Err.Number<>0 then Exit function

    SaveFiles = ""
    ks = Upload.UploadedFiles.keys
    if (UBound(ks) <> -1) then
    response.write "<h3>Upload Success!</h3>"
        SaveFiles = "<B>You have successfully uploaded:</B> "
                
                Dim CurrentTime
                CurrentTime = NOW()
                Dim cn,sql,theFile,sFileTitle
                Set cn = Server.CreateObject("ADODB.Connection")
                cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("FacilitiesPlan.accdb") 
                

        for each fileKey in Upload.UploadedFiles.keys
        
                SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " <br>"
               ' SaveFiles = SaveFiles & "<br>You can view this document here: "    
                'SaveFiles = SaveFiles & "http://www.mywebsite.co.uk/tempUploads/" & Upload.UploadedFiles(fileKey).FileName
        
                
                theFile = Upload.UploadedFiles(fileKey).FileName ' the filename of the actual file
                sFileTitle = Upload.Form(Upload.UploadedFiles(fileKey) & "_name") ' what the user entered in the filename field
    
                'insert query

				
                sql = "Insert into NotedIssues(attachment,AiMWO) values (" _
                & "'" & attach1 & "', " _
                & "'" & AiMWO & ")"
  
                cn.Execute(sql)
        
        next
                    
                'Now that we have displayed the table data lets close the connection
                cn.Close
                Set cn = nothing
        
    else
        SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
    end if
    'SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>"
    'SaveFiles = SaveFiles & "Checkbox values = " & Upload.Form("checkbox_values") & "<br>"
end function
%>

<HTML>
<HEAD>
<TITLE>Test Free ASP Upload 2.0</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
BODY {background-color: white;font-family:arial; font-size:12}
</style>
<script>
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "")
        alert("Please press the Browse button and pick a file.")
    else
        return true;
    return false;
}
</script>

</HEAD>

<BODY>

<br><br>
<div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div>
<%
Dim diagnostics
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
    diagnostics = TestEnvironment()
    if diagnostics<>"" then
        response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
        response.write diagnostics
        response.write "<p>After you correct this problem, reload the page."
        response.write "</div>"
    else
        response.write "<div style=""margin-left:150"">"
        OutputForm()
        response.write "</div>"
    end if
else
    response.write "<div style=""margin-left:150"">"
    OutputForm()
    response.write SaveFiles()
    response.write "<br><br></div>"
end if

%>



</BODY>
</HTML>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of giuseppepi
giuseppepi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FSAAdmin

ASKER

I created a folder Database and still get the same error message but now it points to the new location of the database.  

I also gave Full Control to the guest account.

Microsoft JET Database Engine error '80004005'

Could not find file 'C:\Inetpub\wwwroot\FacilityConditionTracking\Database\FacilitiesPlan.accdb'.

/FacilityConditionTracking/uploadTester3.asp, line 93
I receive a different error after changing my connection string receive a different error.

Microsoft JET Database Engine error '80040e4d'

Cannot start your application. The workgroup information file is missing or opened exclusively by another user.

/FacilityConditionTracking/uploadTester3.asp, line 101

MyConnection.Open MyConnectionString
Dim MyConnection
				Dim MyConnectionString
				Dim MySQLStatement
				
				MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
				"Data Source=C:\Inetpub\wwwroot\FacilityConditionTracking\Database; User ID=;Password="       
				MySQLStatement = "INSERT INTO NotedIssues (AiMWO) Values ('166777')"
				
				Set MyConnection = Server.CreateObject("ADODB.Connection")
				MyConnection.Open MyConnectionString
				MyConnection.Execute(MySQLStatement)
				MyConnection.Close()
				
				Response.Write ("Record inserted")
				Set MyConnection = Nothing

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am able to upload the file and write literal values to the database.  However, now I'm stuck trying to read the variables entered on the form and inserting them into the database.  
Thank you for looking at this.  It turns out the problem was due to several database setup errors including the use of the column "Level" which I found out was a reserved word.  I was also forced to enter a date and programmatically code the value of a checkbox.