473,663 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP - SQL query with where clause causes error if not returned.

13 New Member
Hi,
I'm fairly new to ASP and I want to check a form input against my database to see if an entry with the same name already exists and have an if..then and else clause based on this.
I think I'm going about this the wrong way as I get an error when the database doesn't find a match.

Expand|Select|Wrap|Line Numbers
  1.  
  2. variable = Request.querystring("formname")
  3.  
  4.     Set check = MyConn.Execute("select * from table where name='" & variable &"'")
  5.  
  6.     if check("name")=variable then
  7.  
  8.         response.write("I'm sorry, but the name you specified - " & check("name") & "already exists")
  9.  
  10.     else
  11.         call function()
  12.     end if
  13.  
This code works fine where there is already an entry in the database, but falls over on the if function when there is no match.

Any suggestions much appreciated
Mar 16 '11 #1
2 2639
jhardman
3,406 Recognized Expert Specialist
Good question, phil. Look at your logic. If the query returns anything than that means you have a name conflict. Instead of checking for a name conflict after you run the query (check("name") = variable) check to see if you get anything
Expand|Select|Wrap|Line Numbers
  1. if check.eof then
  2.    'Nothing was returned, so there is no conflict
  3. Else
  4.    'Conflict found
  5. End if
if you're interested, the query returns a "recordset" , in other words, the data looks like this:
Expand|Select|Wrap|Line Numbers
  1. bof
  2. Row of data
  3. Row of data
  4. Row of data
  5. Eof
and then positions the recordset on the first row of data. When you ask for check("name") you are asking the recordset what is the "name" field value of the current row. If you use the check.movenext command you go on to the second row of data etc. If you go on to the eof line and ask for the name field you will get an error because there is no data on that line. When your query returns no results all your recordset receives is "eof". So when you ask for check("name") you are asking it to look in the eof line, and there is no data there.

Let me know if this helps.

Jared
Mar 16 '11 #2
Phil Gent
13 New Member
Thanks for your help Jared, I actually managed to work it out in the end, but your explanation helps me to understand why this works so cheers for that (I did it a little bit backwards, but works in the same way!) Code below

Expand|Select|Wrap|Line Numbers
  1.  
  2. variable = Request.querystring("formname")
  3.  
  4.      Set check = MyConn.Execute("select * from table where name='" & variable &"'")
  5.  
  6.      if not check.EOF then
  7.           response.write("I'm sorry, but the name you specified - " & variable & "already exists")
  8.  
  9.      else
  10.           call function()
  11.  
  12.      end if
  13.  
Mar 17 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2133
by: steve | last post by:
To gain performance, do I need to index ALL the fields in the where clause. Say we have a query like: select stuff from table where field1=.. and field2=... If field1 selection substantially reduces the number of records selected, do we really need to index field2 as well. In other words, does the query first subselect from the first argumet,
1
2948
by: Amir | last post by:
Hi all, I have a table called PTRANS with few columns (see create script below). I have created a view on top that this table VwTransaction (See below) I can now run this query without a problem: select * from dbo.VwTransaction where
2
15762
by: sunny076 | last post by:
Hi, I am confused with the syntax for NOT in MYSQL where clause and wonder if an expert in MYSQL can enlighten me. There are possibly two places NOT can go in: select * from employee_data where NOT employee_id LIKE 'A%' select * from employee_data where employee_id NOT LIKE 'A%'
4
1969
by: Barry Edmund Wright | last post by:
I would really appreciate your assistance. I am using Access 2000 to create a form that Lists Names and Addresses based on a number of selection criteria one of which is a combo box cboPCZip. All the selection criteria are used to build a Where clause (strWhere1). If optBeginsContains = 1 Then 'Return records Beginning with the value in cboPCZip strWhere1 = strWhere1 & " And ((tblAddress.strPCZip) like """ & cboPCZip & "*"")"
2
1495
by: David Smithz | last post by:
Hi, If you run a query which has a WHERE statement in which has a few possibilities separated OR statements, e.g. Select * from table where (Afield = 2) OR (Bfield = 2) OR (Cfield = 2) In the returned results is it possible to know what particular part of the WHERE clause a field matched.
2
10747
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where it is used. SQLSTATE=42703 SELECT DISTINCT S3.OPR_APPLICATION_NR, S3.APPLICATION_ID, S3.APPRAISAL_TYPE_CD, S3.Appraisal_Used_Amount, S3.RPT_LEVEL2_NR,
9
19139
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ------------------------- SELECT * FROM ( Select x.event_date From x FULL OUTER JOIN y ON x.event_date = y.event_date
8
3475
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 , etc. I want to go to the first row, do a WHERE statement, return the
9
2819
by: Yitzak | last post by:
Hi spent a few hours on this one wrote a query that joined on results of 2 other queries. Qry3 using Qry1 and Qry2 When I used Qry1.FasText <cstr(Qry2.FasInteger) in the where clause - got invalid use of null. Fair enough Field1 and Field2 can contain nulls I altered the 2 queries to exclude nulls from FasText and FasInteger-
3
2250
by: Vivekneo | last post by:
Hi, iam trying to insert values in the child table, with condition that both pk and fk should match, pk = stud_id and fk dstud_id. here is what iam trying include('config.php'); if (isset($_GET) ) { $id = (int) $_GET; if (isset($_POST)) { foreach($_POST AS $key => $value) { $_POST = mysql_real_escape_string($value); }
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.