0

I am trying to check if a record exist in the database. If it exists discard insert function. And if the record does not exist insert.

I'm getting the opposite effect.

<% 
Set OBJdbConnection = Server.CreateObject("ADODB.Connection")  
OBJdbConnection.open MM_Listings_STRING

'1. Check if contractor is linked
SQLQuery1 = "Select * FROM Agent_Contractor WHERE ContractorID = " &_
Request.Form("ContractorID") & " AND AgentID = " & Session("AgentID") 

Set Results = OBJdbConnection.Execute(SQLQuery1)

'1. If linked response write. 
If Results.EOF  Then
  Response.Write "Contractor Already Linked"
else
  '1. Insert
  SQLQuery3 = "INSERT INTO Agent_Contractor (" &_
  "ContractorID, " &_
  "AgentID) VALUES (" &_
  Request.Form("ContractorID") & ", " &_
  Session("AgentID") & ")"

  OBJdbConnection.Execute(SQLQuery3)    
  Response.Write "Contractor Linked successfully."
end if
%>
3
  • 1
    Your code is full of security holes.
    – ElGavilan
    Commented May 28, 2014 at 18:45
  • What @ElGavilan is saying is your code is vulnerable to SQL Injection consider using the ADODB.Command object to build a Parametrised Query, instead of using ADODB.Connection's Execute() method.
    – user692942
    Commented May 29, 2014 at 7:39
  • Also if you are just checking for the existence of the contractor there is no need to return all the fields from Agent_Contractor table consider using SELECT NULL FROM Agent_Contractor ... the query will still work but will not return any field, just NULL which still signifies the existence of the record without the overhead of using SELECT * (Assuming your using SQL Server for your db, not specified in your question or tags).
    – user692942
    Commented May 29, 2014 at 7:46

2 Answers 2

5

You statements are backwards in your code.

"If either the BOF or EOF property is True, there is no current record." as per http://msdn.microsoft.com/en-us/library/office/ff821459(v=office.15).aspx

So the contractor is NOT linked if Results.EOF are true.

1
  • @user3501463 This is the correct answer your issue regardless of the method you have used is your check for data in the ADODB.Recordset. You should be using If Not Results.EOF Then (Your logic is back to front at present). +1
    – user692942
    Commented May 29, 2014 at 7:43
0

You can use NOT EXISTS condition in WHERE. Take a look at the sample code below:

"INSERT INTO Agent_Contractor (ContractorID, AgentID) " &_
"SELECT '" & Request.Form("ContractorID") & "', '" &_
Session("AgentID") & "' FROM Agent_Contractor WHERE " &_
"NOT EXIST (SELECT * FROM Agent_Contractor WHERE ContractorID = '" &_
Request.Form("ContractorID") & "' AND AgentID = '" & Session("AgentID") & "')"

I'm not sure if I get the cancatination right for I can't run it but I'm trying to point out the logic. You can make an adjustment to that.

1
  • 1
    We shouldn't be encouraging bad practice, this statement is vulnerable to SQL Injection. At least mention this in your answer. Also don't think reversing the logic in the SQL is the way to fix this issue.
    – user692942
    Commented May 29, 2014 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.