Solved

OLEDB command parameters not being assigned in order in SQL string

Posted on 2006-07-01
4
217 Views
Last Modified: 2011-04-14
The parameters for this OLEDB command are not being placed in the order in which they appear in the SQL string, probably due to nesting.  How can I determine the order in which they are being applied?

        strSql = "SELECT Subjects.SubjectID, Subjects.BusinessName, Subjects.City, Subjects.StateCode FROM (Subjects) WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE ? > 3959 * ATN( SQR(1 - (SIN(?/ 57.3) * SIN(LAT/ 57.3) + COS(?/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (?/ 57.3))) ^2 ) / (SIN(?/ 57.3) * SIN(LAT/ 57.3) + COS(?/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (?/ 57.3))) ) )))"
        objCmd = New OleDbCommand(strSql, objConn)
        setParam(objCmd, "Rad", OleDbType.Numeric, vRad)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lng", OleDbType.Numeric, vLng)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lng", OleDbType.Numeric, vLng)

    Protected Sub setParam(ByRef objCmd As OleDbCommand, ByVal strParamName As String, ByVal varType As OleDbType, ByVal varValue As Object)
        objCmd.Parameters.Add(strParamName, varType).Value = varValue
    End Sub

I know they aren't being applied in the correct order because when I take the variables as supplied to the command in the web application:
vRad = 50
vLat = 40.2411
vLng = 76.0567

and then apply them explicitly in a SQL statement to the Access database:
SELECT Subjects.BusinessName, Subjects.City, Subjects.StateCode
FROM Subjects
WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE 50 > 3959 * ATN( SQR(1 - (SIN(40.2411/ 57.3) * SIN(LAT/ 57.3) + COS(40.2411/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (76.0567/ 57.3))) ^2 ) / (SIN(40.2411/ 57.3) * SIN(LAT/ 57.3) + COS(40.2411/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (76.0567/ 57.3))) ) )));

I get 156 results, not the 5000+ returned by the OLEDB command.

I'm thinking there might be a way to surround one parameter at a time in the SQL string with some kind of internal function which will cause certain values to fail, and then supply test values one at a time to the command to determine the location of each ordered parameter, but I've already spent too much time on trial-and-error and think this is a job for.. the Experts Exchange!
0
Comment
Question by:psk1
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 17024373
Access should let you force the parameter ordering by adding a parameter clause to your sql and then using the named parameters in your sql:

   strSql = "PARAMETERS myrad Long, mylat IEEEDouble, mylon IEEEDouble; SELECT Subjects.SubjectID, Subjects.BusinessName, Subjects.City, Subjects.StateCode FROM (Subjects) WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE [myrad] > 3959 * ATN( SQR(1 - (SIN([mylat]/ 57.3) * SIN(LAT/ 57.3) + COS([mylat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([mylon]/ 57.3))) ^2 ) / (SIN([mylon]/ 57.3) * SIN(LAT/ 57.3) + COS([mylat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([mylon]/ 57.3))) ) )))"

You'll still have to add them in the order you've defined them in your parameter clause:

  setParam(objCmd,"rad", OleDbType.Int32, vRad)
  setParam(objCmd,"lon", OleDBType.Double, vLon)
  setParam(objCmd,"lat", OleDBType.Double, vLat)

Give it a try.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17024379
Sorry... I can't even manage to keep them in the same order.  Time to pack it in:

  setParam(objCmd,"rad", OleDbType.Int32, vRad)
  setParam(objCmd,"lat", OleDBType.Double, vLat)
  setParam(objCmd,"lon", OleDBType.Double, vLon)
0
 
LVL 2

Author Comment

by:psk1
ID: 17025236
You did it.  Thanks (again) clockwatcher.
0
 
LVL 2

Author Comment

by:psk1
ID: 17025241
Seriously, thanks.  You've saved me much time and confusion and helped to keep the ball rolling.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now