Wrox Programmer Forums
|
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Pro Code Clinic 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 August 21st, 2003, 11:42 AM
Authorized User
 
Join Date: Jun 2003
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extra spaces in variables

Hi All,

I am trying to upload scanned jpg receipts to the database and then putting a reference into the database so that you can then clip an icon and display the receipt. The upload works perfectly however using the same data past from the previous form which is submitted as enctype="multipart/form-data" I then get additional spaces in the variable which is used to query the database. Because it has extra space padding e.g. " 00001 " the entry in the database is not found and the it is not updated. I now will so the coding:

<%@ LANGUAGE="VBScript" %>




<%
    Response.Buffer = True

    Dim load
        Set load = new Loader
        load.initialize

    ' File binary data
    Dim fileData, fileName, filePath, filePathComplete, fileSize, fileSizeTranslated, contentType
    Dim countElements, nameInput, pathToFile, filuploadfacteUploaded, strSQL, dbDir, varDate
        varDate = Now()
        fileData = load.getFileData("file")
        fileName = LCase(load.getFileName("file"))
        filePath = load.getFilePath("file")
        filePathComplete = load.getFilePathComplete("file")
        fileSize = load.getFileSize("file")
        fileSizeTranslated = load.getFileSizeTranslated("file")
        contentType = load.getContentType("file")
        countElements = load.Count
        rsName = load.getValue("name")
        nameInput = load.getValue("name")
        pathToFile = Server.mapPath("fac/") & "\" & fileName
        fileUploaded = load.saveToFile ("file", pathToFile)
    Set load = Nothing

    DIM FFile
        FFile = "sf_" & rsName & ".jpg"
        rsName = Trim(rsName)
        set DbConn = Server.CreateObject("ADODB.Connection")
        DbConn.Open "DSN=sii"

        strSQL = "UPDATE Facture SET" _
               & " [FScan] = '" & FFile & "'," _
               & " [FScanDate] = '" & varDate & "'" _
               & " WHERE FactureNum = '" & rsName & "';"
        response.write "FFile = " & FFile & "<br>"
        response.write "varDate = " & varDate & "<br>"
        response.write "nameInput = <" & rsName & ">"
        Response.End
        DbConn.Execute(strSQL)

%>

At the bottom of the code you can see where I have been testing the system and I discovered the extra spaces. I have tried using the vbscript command TRIM() but without success.

Is there a way around this problem.

Please help
With many thanks for your time and support,

Paul Jacobs
 
Old August 22nd, 2003, 03:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Trim will only get rid of leading and trailing spaces, not zeros. Convert it to an int then back to a string and that will sort it out:
rsName = CStr(CInt(rsName))
 
Old August 22nd, 2003, 05:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CInt will cause an error if the value in rsName is not numeric. You may want to add the IsNumeric test to ensure that this does not happen:

Code:
If IsNumeric(rsName) Then
    rsName = CStr(CInt(rsName))
Else
    rsName = Trim(rsName)
End If
One other point, in your code listing you do not declare the rsName variable. Is it in one of the include files or have you just forgotten to declare it? When testing you may find it helpful to add the Option Explicit statement:

Code:
<%@ LANGUAGE="VBScript" %>
<%Option Explicit%>




<%
    Response.Buffer = True
...
This will ensure that all your variables are declared, otherwise an error will occur. You should remove the Option Explicit statement once your site goes live because it will increase the compile time of your code. This is because it must scan through the code to check that everything has been declared before it will compile it.

Regards
Owain Williams
 
Old August 22nd, 2003, 11:36 AM
Authorized User
 
Join Date: Jun 2003
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Using this code appears to work in the fact that it removes the bounding spaces but now I have another problem created by the code suggested.

The receipt number is for example 00001 and using the function suggested it has converted it from " 00001 " to "1", eliminating all the zeros. In the database the number is required with zeros so it is not finding the correct number to update.

Any suggestions...

With many thanks...

Paul D. Jacobs
Web Applications Developer and Information Systems Manager
[email protected]
  _____

Servicios Interactivos Internacionales S.A. de C.V.
Via Ceti #56, Fracc Luis Enrique Erro,
Planetario Lindavista,
C.P 07730
México D.F.
 
Old August 23rd, 2003, 06:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In that case, I can only assume that the spaces are not spaces, and are in fact special characters (character 0's for example). Trim will get rid of all leading and trailing spaces (character 32's) only, it will not get rid of any other non displayable characters. You will have to go through the string and convert any non displayable characters into spaces. There is probably a quicker way of doing this (with Regular expressions for example) however this code will work:

Code:
If Len(rsName) > 0 Then
    For intCount = 1 to Len(rsName)
        If Asc(Mid(rsName, intCount, 1)) <= 32 Then
            rsName = Left(rsName, intCount - 1) & " " & Mid(rsName, intCount + 1)
        End If
    Next
End If
rsName = Trim(rsName)
You may also need to get rid of characters that are at the other end of the spectrum. Or if you know it will only ever be numbers then you can turn anything that is not a number into a space.

Regards
Owain Williams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Spaces XP Programmer911 General .NET 1 December 19th, 2007 07:03 AM
Extra Spaces Added When Inserting rsearing ASP.NET 2.0 Basics 3 January 8th, 2007 10:56 AM
disappearing spaces gezi XSLT 0 March 26th, 2006 02:23 AM
Frame Spaces EvilGuyWhoEatsBrains HTML Code Clinic 2 January 5th, 2005 12:24 PM





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