If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > How to check if record in database exists?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-03, 03:40
johnan johnan is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
How to check if record in database exists?

Hi ppl, I'm trying to make a program where users fill out a form and submit.I've no problems inserting data into the database(sql 2000)

However, records must be unique.
Example: Category1 + Category2 + Category3 must be unique in the database.

So how do I check when users fill in the same data and submit to the database?
I'd like to inform users that the record already exists.

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 08-12-03, 16:51
J_C J_C is offline
Registered User
 
Join Date: Aug 2003
Location: Canada
Posts: 6
Talking

I am not quite sure what do you mean by unique "Category1 + Category2 + Category3".

However, if you just want to make sure the same data doesn't go into the database twice, can't you just first query for the data that the user try to put in, and if it returns nothing(meaning the data is not in the db), then you put it in?
Reply With Quote
  #3 (permalink)  
Old 08-12-03, 23:43
johnan johnan is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Oh let me try to explain.
I have a table named Category in a Database with fields Cat1,Cat2,Cat3.

Lets say there is a record already in the database with Cat1=a, Cat2=b, Cat3=c.

And a user tries to submit a form with the exact same values, I wish to query the database and check the values in the fields entered.
I'm not too sure how to go about it.The following is codes that check ONE field only and I still cant get it to work.

rsSQL = "SELECT Category1 FROM Category WHERE Category1 = '" & cat1 & "'"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open rsSQL, DB

Do While Not rs.EOF
if rs("Category1") = cat1 then
strExist = "yes"
strCat1 = rs("category1")
end if
rs.MoveNext
Loop

rs.close
set rs=nothing

If strExist = "yes" then
response.write ("Record Exists.<a href='javascript: history.back()'>Back</a>")
response.write(strCat1)
end if
Reply With Quote
  #4 (permalink)  
Old 08-13-03, 01:08
Memnoch1207 Memnoch1207 is offline
Registered User
 
Join Date: Jan 2003
Location: Midwest
Posts: 138
Code:

rsSQL = "SELECT COUNT(*) As count FROM Category WHERE Category1 = '" & cat1 & "' AND Category2 = '" & cat2 & "' AND Category3 = '" & cat3 & "'"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open rsSQL, DB

if(rsSQL("count") > 0) then
     strExists = "yes"
else
     strExists = "no"
end if

rs.close
set rs=nothing

If strExist = "yes" then 
response.write ("Record Exists.<a href='javascript: history.back()'>Back</a>")
response.write(strCat1)
end if
Reply With Quote
  #5 (permalink)  
Old 08-14-03, 00:07
johnan johnan is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Woohoo I modified the codes a little and now it works~
Thanks for all your help! ^^
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On