Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: type mismatch

  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    type mismatch

    I really can't figure this out. I have images in the folder.

    Code:
    <%
    function move_files(root,whereto,upnum,datetime,modified)
    	on error resume next
    	'Prepare variables and objects
    	dim fs, folder, file, item
    	
    	'Create scripting object and open root folder
    	set fs = CreateObject("Scripting.FileSystemObject")
    	set folder = fs.GetFolder(root)
    	
    	'Check that the root directory is found
    	if folder = false then
    		response.write "Error: Upload stopped, root directory not found, ensure storage device is still plugged in!"
    		else 
    		'Move the files in the root first
    		for each item in folder.files
    			tmpjpg = item.name
    			
    			'File moved, add to database
    			sql="'" & tmpjpg
    				sql=sql & "'," & upnum
    				sql=sql & ",0,"&datetime
    				sql=sql & ","&modified
    			
    			'Add to database
    			handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    			if handle = true then
    				response.write "Error: Upload stopped (1), error adding to database: " & handle & "<br />"
    				'response.end
    			else 
    				'Move the file if it has been added to the database
    				fs.MoveFile folder&"\"&tmpjpg, whereto
    				if err.number <> 0 then
    					response.write "Error: Upload stopped (2), error code: "&err.description&" | "&folder&"\"&tmpjpg & "<br />"
    				end if
    			end if
    		next
    		
    		'Now search through the sub folders and move the files
    		if folder.SubFolders.count > 0 then
    			for each item in folder.SubFolders
    				'Open new foler
    				set subfolder = fs.GetFolder(root&item.name)
    				for each subfile in subfolder.files
    					
    					'Move the files
    					'fs.MoveFile subfolder&"\"&subfile.name, whereto
    					
    					'Check for errors
    					if err.number <> 0 then
    						response.write "Error: Upload stopped (3), error code: "&err.description
    						response.end
    					else
    						'File moved, add to database
    						sql="'" & subfile.name
    							sql=sql & "'," & upnum
    							sql=sql & ",0,"&datetime
    							sql=sql & ","&modified
    		
    						'Add to database
    						handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    						if handle = true then
    							response.write "Error: Upload stopped (4), error adding to database: " & handle
    							response.end
    						end if
    					end if
    				next
    			next
    		end if
    		
    		'Check for errors
    		If Err.Number <> 0 Then
    			move_files = "Error: Unexpected - "&err.description
    		End If
    	end if
    end function
    
    result = move_files("D:\DCIM\","D:\Moved\",0,#12/07/2013 5:12#,#12/07/2013 2:12#)
    response.write result
    
    %>
    Here is the result displayed:
    Code:
    Error: Upload stopped (2), error code: Type mismatch | D:\DCIM\IMG_1106.JPG
    Error: Unexpected - Type mismatch

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    I truly do *NOT* think you are getting a Type Mismatch error from the code you think it is coming from.

    Because of the way you are checking for Err.Number, you don't *KNOW* that the error occurred on the statement just before that check!!!

    If you do Err.Clear just before attempting a statement, *THEN* you will be able to be sure whether or not the error is really on that line.

    But the real problem is the indiscriminate way you are using ON ERROR RESUME NEXT.

    In my opinion, you should *NEVER* use it at the top of a page or function and then just leave it turned on.

    Instead, you should turn it on and off (turn off via ON ERROR GOTO 0), leaving it on *ONLY* for statements where you really expect that an error is (a) likely and (b) a spot where you can actually do something *ABOUT* the error.

    So I would perhaps have written that part of the code thus:
    Code:
    	'Check that the root directory is found
    	if folder = false then
    		response.write "Error: Upload stopped, root directory not found, ensure storage device is still plugged in!"
    	else 
                    On Error GoTo 0
    		'Move the files in the root first
    		for each item in folder.files
    			tmpjpg = item.name
    			
    			'File moved, add to database
    			sql="'" & tmpjpg
    				sql=sql & "'," & upnum
    				sql=sql & ",0,"&datetime
    				sql=sql & ","&modified
    			
    			'Add to database
    			handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    			if handle = true then
    				response.write "Error: Upload stopped (1), error adding to database: " & handle & "<br />"
    				response.end ' DEFINITELY do NOT keep going after such an error!
    			else 
    				ON ERROR RESUME NEXT
    				'Move the file if it has been added to the database
    				fs.MoveFile folder&"\"&tmpjpg, whereto
    				if err.number <> 0 then
    					response.write "Error: Upload stopped (2), error code: "&err.description&" | "&folder&"\"&tmpjpg & "<br />"
    				end if
    				ON ERROR GOTO 0
    			end if
    		next
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-17-2013)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Code:
    Microsoft VBScript runtime error '800a000d'
    
    Type mismatch: 'add_to_database'
    
    /nightclub_photography/testing.asp, line 32
    This is the line
    Code:
    handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    So where is the code for add_to_database?

    That probably means you have the wrong number or type of arguments when calling that function, but without seeing the code for it I can't possibly tell.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    So where is the code for add_to_database?

    That probably means you have the wrong number or type of arguments when calling that function, but without seeing the code for it I can't possibly tell.
    Code:
    function add_to_database(where,column,values)
    	'Create connection
    	set conn=Server.CreateObject("ADODB.Connection")
    	conn.Provider="Microsoft.ACE.OLEDB.12.0"
    	conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
    	
    	'Check content is safe
    	'safe_values = strip_special(values)
    	
    	'Create query
    	sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
    	on error resume next
    	'Add images to database
    	conn.Execute sql,recaffected
    	
    	if err<>0 then
    		add_to_database = "Unable to add "&jpg_name(0)&" to database! "&Err.Description&"<br />"&sql&"<br /><br />"
    	else
    		add_to_database = true
    	end if
    	
    	'Destroy connection
    	conn.close
    	sql = ""
    end function

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Again, creating the connection in this function and then closing it each time is a bad idea. You should create the connection at the top of the page, use it throughout the page, and then close it at the end of the page.

    But that's not your error.

    I'm more than curious: You have this error msg
    add_to_database = "Unable to add "&jpg_name(0)&" to database! "&Err.Description&"<br />"&sql&"<br /><br />"

    Where in the heck is the variable jpg_name coming from? It's clearly not defined in that function.

    But I don't think that's your error.

    I guess the first thing I would do is make sure the error isn't coming *FROM* the function.

    So *TEMPORARILY* just change the function to this:
    Code:
    Function add_to_database(where,column,values)
        add_to_database = true
    End Function
    If the error persists, then you have some problem with code ordering, though I don't see what it is.

    If the error goes away, then it must actually be someplace inside that function and is simply not showing until you get back out of it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-18-2013)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Done that, I get the same error.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Totally mystified. I created a dummy ASP page with that same function name and passed it the same parameters you are using...no problem!

    My head hurts.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    I really can't see the issue with it! I'm guessing it's the fact i'm trying to return the value of the function to a variable.

    Here is my entire code
    Code:
    <html>
    <head>
    <title>Testing ASP code</title>
    </head>
    <body>
    <%
    function move_files(root,whereto,upnum,datetime,modified)
    	'Create connection
    	set conn=Server.CreateObject("ADODB.Connection")
    	conn.Provider="Microsoft.ACE.OLEDB.12.0"
    	conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
    	
    	on error resume next
    	'Prepare variables and objects
    	dim fs, folder, file, item
    	
    	'Create scripting object and open root folder
    	set fs = CreateObject("Scripting.FileSystemObject")
    	set folder = fs.GetFolder(root)
    	
    	'Check that the root directory is found
    	if folder = false then
    		response.write "Error: Upload stopped, root directory not found, ensure storage device is still plugged in!"
    	else 
    		on error goto 0
    		'Move the files in the root first
    		for each item in folder.files
    			tmpjpg = item.name
    			
    			'File moved, add to database
    			sql="'" & tmpjpg
    				sql=sql & "'," & upnum
    				sql=sql & ",0,"&datetime
    				sql=sql & ","&modified
    			
    			'Add to database
    			handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    			if handle = true then
    				response.write "Error: Upload stopped (1), error adding to database: " & handle & "<br />"
    				'response.end
    			else 
    				on error resume next
    				'Move the file if it has been added to the database
    				fs.MoveFile folder&"\"&tmpjpg, whereto
    				if err.number <> 0 then
    					response.write "Error: Upload stopped (2), error code: "&err.description&" | "& folder&"\"&tmpjpg & "<br />"
    				end if
    				on error goto 0
    			end if
    		next
    		
    		'Now search through the sub folders and move the files
    		if folder.SubFolders.count > 0 then
    			for each item in folder.SubFolders
    				'Open new foler
    				set subfolder = fs.GetFolder(root&item.name)
    				for each subfile in subfolder.files
    					
    					'Move the files
    					'fs.MoveFile subfolder&"\"&subfile.name, whereto
    					
    					'Check for errors
    					if err.number <> 0 then
    						response.write "Error: Upload stopped (3), error code: "&err.description
    						response.end
    					else
    						'File moved, add to database
    						sql="'" & subfile.name
    							sql=sql & "'," & upnum
    							sql=sql & ",0,"&datetime
    							sql=sql & ","&modified
    		
    						'Add to database
    						handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    						if handle = true then
    							response.write "Error: Upload stopped (4), error adding to database: " & handle
    							response.end
    						end if
    					end if
    				next
    			next
    		end if
    		
    		'Check for errors
    		If Err.Number <> 0 Then
    			move_files = "Error: Unexpected - "&err.description
    		End If
    	end if
    	conn.close
    end function
    
    result = move_files("D:\DCIM\","D:\Moved\",0,#12/07/2013 5:12#,#12/07/2013 2:12#)
    response.write result
    
    %>
    </body>
    </html>
    And the entire function
    Code:
    function add_to_database(where,column,values)	
    	'Create query
    	sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
    	on error resume next
    	'Add images to database
    	conn.Execute sql,recaffected
    	
    	if err<>0 then
    		add_to_database = true
    	end if
    	
    	'Destroy connection
    	sql = ""
    end function

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Ummm... where the heck *IS* the function???

    In the first set of code, just above, you showed the entire ASP page, from <html> to </html>.

    But you don't show the add_to_database function anywhere within there.

    It has to be on the same page in order to be called, you know.

    What happens if you put it *just before* the move_files function?

    And, while you are at it, move the connection open/close to start/end of page, as I keep saying:
    e.g.,:
    Code:
    <html>
    <head>
    <title>Testing ASP code</title>
    </head>
    <body>
    <%
    ' Create connection at START OF PAGE
    set conn=Server.CreateObject("ADODB.Connection")
    conn.Provider="Microsoft.ACE.OLEDB.12.0"
    conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
    
    function add_to_database(where,column,values)	
    	'Create query
    	sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
    	on error resume next
    	'Add images to database
    	conn.Execute sql,recaffected
    	
    	if err<>0 then
    		add_to_database = true
    	end if
    	
    	'Destroy connection
    	sql = ""
    end function
    
    function move_files(root,whereto,upnum,datetime,modified)
    	'Prepare variables and objects
    	dim fs, folder, file, item
    	
    	'Create scripting object and open root folder
    	set fs = CreateObject("Scripting.FileSystemObject")
    	set folder = fs.GetFolder(root)...
            ...
    		If Err.Number <> 0 Then
    			move_files = "Error: Unexpected - "&err.description
    		End If
    	end if
    	' conn.close **** do *NOT* do this here! ****
    end function
    
    result = move_files("D:\DCIM\","D:\Moved\",0,#12/07/2013 5:12#,#12/07/2013 2:12#)
    response.write result
    
    conn.Close ' and close connection as last thing
    %>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-20-2013)

  • #11
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    OMG! FFS! I could of sworn there was an include at the top of the page to include the functions!

    There is the problem.. :/

    *facepalm*

  • #12
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    That works fine now, another issue.

    Code:
    'Now search through the sub folders and move the files
    		if folder.SubFolders.count > 0 then
    			for each item in folder.SubFolders
    				on error resume next
    				
    				'Open new foler
    				set subfolder = fs.GetFolder(root&item.name)
    				
    				'Check folder has been opened
    				if err.number <> 0 then
    					response.write "Error: Unable to open sub-folders (3): "&err.description
    				end if
    				on error goto 0
    				
    				for each item in subfolder
    					sql="'" & item.name
    						sql=sql & "'," & upnum
    						sql=sql & ",0,"&datetime
    						sql=sql & ","&modified
    ...
    ...
    As you can see i'm using a "for" loop inside a "for" loop. I need to use the "item" keyword for both of these loops, will it get confused as to which item is being referred to?

    Getting the following error:
    Code:
    Microsoft VBScript compilation error '800a0410'
    
    Invalid 'for' loop control variable
    
    /nightclub_photography/testing.asp, line 68
    
    for each item in subfolder
    --------------^

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Code is nonsensical.

    You can't do a FOR EACH on a folder object.

    You need to use the FILES collection.

    As in
    Code:
        For Each item In subfolder.Files
    But item is a poor choice of name, as the FILES collection itself is made up of ITEMs. I prefer using some like
    Code:
        For Each fileobj In subfolder.Files
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-21-2013)

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    One more time: Have you never downloaded the Scripting Help files that I pointed you to? The example in there on the FOLDER object and then the FILES collection would have shown you exactly the code to use.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-21-2013)

  • #15
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    One more time: Have you never downloaded the Scripting Help files that I pointed you to? The example in there on the FOLDER object and then the FILES collection would have shown you exactly the code to use.
    Okay thanks, and I still ain't read it. Been reading C# For Dummies, I need to get around to reading some ASP stuff.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •