-1

I got a query using SQL inside ASP Classic, and the syntax that I got is shown below:

    mQry = "SELECT name FROM finals WHERE invoice_num = " &request.querystring("num") & " AND name LIKE '&" & request.querystring("nam") & "%'"
    response.write("Operating Unit")
    for each x in TestRally.fields
    response.write(": ")
    response.write(x.value)  '-- got error in this area...
    response.write("<br>")
    next
    response.write("<br>")
    TestRally.MoveNext

Based on the above mentioned syntax, what would be the best possible remedy for this..???

The error message I got so far is this:

Microsoft OLE DB Provider for Oracle error '80040e07' ORA-01722: invalid number

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388

4 Answers4

2

Your code is wide-open to SQL injection attacks and that is partly the cause of your problem: you're not verifying (let alone sanitizing) your input that you use to formulate queries.

See this QA before continuing: What is SQL injection?

We can't tell you how to properly solve it without knowing what DBMS you're using, but a quick workaround is this:

Dim invoiceNum
invoiceNum = CInt( Request.QueryString("num") )
Dim name
name = Request.QueryString("nam")
name = Replace( name, "'", "''" )

Dim sql
sql = "SELECT name FROM finals WHERE invoice_num = " & invoiceNum & " AND name LIKE '&" & name & "%'"
Community
  • 1
  • 1
Dai
  • 110,988
  • 21
  • 188
  • 277
0
invoiceNum = CInt( Request.QueryString("num") ) 

is probably causing the overflow.

The invoice number you are passing is too big for an integer. Save the invoice number as string.

Using Dai's code, try this:

Dim invoiceNum
invoiceNum = Request.QueryString("num")
invoiceNum = Replace( invoiceNum, "'", "''")

Dim name
name = Request.QueryString("nam")
name = Replace( name, "'", "''" )

sql = "EXEC sp_executesql N'SELECT name FROM finals WHERE invoice_num = @invoice_num AND name LIKE ''&'' + @name + ''%'''"
sql = sql & ",N'@invoice_num varchar(20),@name varchar(255)',@invoice_num = '" & invoiceNum & "',@name = '" & name & "'"
Jin
  • 9
  • 1
  • You advice a wrong way to build a query concatenating parameter values to the command string instead of using parameters. Doing that is wrong and puts the security of your database in risk. – Yván Ecarri Oct 13 '13 at 08:52
0

Enable sql tracing or better 10046 tracing before executing the above statement and you will see which value is passed in for the invoice_num. This will create a trace file that details all the statements and how they are executed.

In addition post how the table has been defined.

steve
  • 5,416
  • 1
  • 16
  • 20
0

"SELECT name FROM finals WHERE invoice_num='" & request.querystring("num") & "' AND name = '" & request.querystring("nam") & "'"

This is the right SQL syntax for the query inside the ASP Classic instead of the above-mentioned SQL query that I have in my question.