Using SQL Parameters in ASP CLASSIC, object improperly defined error

后端 未结 1 560
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 12:03

I am trying to protect my INSERT statement from SQL injection using Parameters, but for some reason I am getting the error: Parameter object is improperly defined. Inconsi

相关标签:
1条回答
  • 2021-01-26 12:23

    Alright, after much discussion with Lankymart, which continued in the chat, I finally got it fixed.

    Because the error was not fixed with just one adjustment, ill post all the adjustments made.

    • First of all I removed the first (unnecessary) parenthesis of spSQL.Parameters.Append(spSQL.CreateParameter("@Order", adInteger,,,1506))
    • Secondly, I replaced the @vars in my SQL string with question marks.
    • Then I separately added the Parameters values and also added the spSQLCommandType = adCmdText (pointed out in this link: stackoverflow.com/a/22037613/692942)

    • I also changed the SIZES of all the parameter data types to the right size (using this link: Data type mapping) instead of default nothing or 0.

    • The biggest problem however was caused by not including the right DDL file for handling my ADO parameters. This was added in the global.asa file. <!-- METADATA TYPE="typelib" UUID="00000200-0000-0010-8000-00AA006D2EA4" -->

    • A few smaller problems remained with one of them being a error on the execute which was changed to: Call spSQL.execute(adExecuteNoRecords)
    • The last problem was caused because adDate wasn't recognized or viable for my SQL server 2012. I changed the ADO type adDate to adDBTimeStamp which solved the problem.

    The entire 'fixed' code is as follow:

    Set spSQL = Server.CreateObject("ADODB.Command")
                    Set spSQL.ActiveConnection=con_vhs
    
                    spSQL.CommandType = adCmdText
    
                    vrdSQL="INSERT INTO boekingen ([Order],[Positie],[Tariefnummer],[Relatie],[Datum],[AantalEenheden],[Omschrijving],[Bedrag],[Totaal],[Status]) VALUES (?,?,?,?,?,?,?,?,?,?)"
                    spSQL.commandtext= vrdSQL
    
                    spSQL.Parameters.Append spSQL.CreateParameter("@Order",adInteger,adParamInput,4)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Positie", adVarWChar,adParamInput,10)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Tariefnummer", adVarWChar,adParamInput,50)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Relatie", adInteger,adParamInput,4)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Datum", adDBTimeStamp,adParamInput,0)
                    spSQL.Parameters.Append spSQL.CreateParameter("@AantalEenheden", adSingle,adParamInput,4)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Omschrijving", adVarWChar,adParamInput,150)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Bedrag", adDecimal,adParamInput,0)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Totaal", adDecimal,adParamInput,0)
                    spSQL.Parameters.Append spSQL.CreateParameter("@Status", adInteger,adParamInput,4)
    
                    spSQL.Parameters("@Order").Value = 1506
                    spSQL.Parameters("@Positie").Value = "0"
                    spSQL.Parameters("@Tariefnummer").Value = "VRD"
                    spSQL.Parameters("@Relatie").Value = 4020
                    spSQL.Parameters("@Datum").Value = iDatumTotaal
                    spSQL.Parameters("@AantalEenheden").Value = TestAantal
                    spSQL.Parameters("@Omschrijving").Value = OmschrijvingGoed
                    spSQL.Parameters("@Bedrag").Value = sBedrag
                    spSQL.Parameters("@Totaal").Value = sTotaal
                    spSQL.Parameters("@Status").Value = StatusVRD
    
                    Dim oPrm
    
                    For Each oPrm In spSQL.Parameters
                        If oPrm.Type = adDecimal Then
                            oPrm.NumericScale = 2
                            oPrm.Precision = 17
                        End If
                    Next
    
    
                    Call spSQL.execute(adExecuteNoRecords)
    

    Thanks to Lankymart for the awesome help fixing this problem!

    0 讨论(0)
提交回复
热议问题