473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

✓ answered by jhardman

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

2 2626
jhardman
3,406 Expert 2GB
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
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
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...
1
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...
2
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...
4
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...
2
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...
2
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...
9
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 ...
8
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 ,...
9
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.