Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for duplicate records in sql database

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi guys, i'm trying to write an if then statement that checks to see if a name is already in my sql database. If it is i want to go back to my form and say tha name is in there. i've written the code but it's still inserting duplicate names in the database. Here's the code, any help would be greatly appreciated.

<%@ LANGUAGE=&quot;VBSCRIPT&quot; %>
<%
Option Explicit
Response.Buffer = True
Response.ContentType = &quot;text/html; charset=iso-8859-1&quot;

'------------------------------------------------------------------------------
'--- Module: SubmitCust.asp
'---
'--- Version: 1.0
'---
'--- Purpose: This will take the values inputted on the Customers form,
'--- and insert them into the correct database tables.
'---
'--- Dependencies:
'---
'--- Notes:
'---
'--- Author: Gene Magerr genemagerr@hotmail.com
'--- Copyright 2001 Magerr Media all rights reserved
'------------------------------------------------------------------------------
%><!--#include file=&quot;../../adovbs.inc&quot;--><%
'------------------------------------------------------------------------------
Dim cnCust, rsCust

'------------------------------------------------------------------------------
'--- Declarations
'------------------------------------------------------------------------------
Set cnCust = Server.CreateObject(&quot;ADODB.Connection&quot;)
cnCust.Open &quot;DSN=Fastlink;uid=sa;pwd=;database=Fastlink&quot;

Set rsCust = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rsCust.Open &quot;Customers&quot;, cnCust, adOpenStatic, adLockOptimistic

If rsCust(&quot;CustCompanyName&quot;) <> Request.Form(&quot;CustCompanyName&quot;) Then
rsCust.addnew

rsCust(&quot;CustCompanyName&quot;) = Request.Form(&quot;CustCompanyName&quot;)
rsCust(&quot;CustContactName&quot;) = Request.Form(&quot;CustContactName&quot;)
rsCust(&quot;CustContactTitle&quot;) = Request.Form(&quot;CustContactTitle&quot;)
rsCust(&quot;CustAddress&quot;) = Request.Form(&quot;CustAddress&quot;)
rsCust(&quot;CustCity&quot;) = Request.Form(&quot;CustCity&quot;)
rsCust(&quot;CustState&quot;) = Request.Form(&quot;CustState&quot;)
rsCust(&quot;CustZip&quot;) = Request.Form(&quot;CustZip&quot;)
rsCust(&quot;CustEmail&quot;) = Request.Form(&quot;CustEmail&quot;)
rsCust(&quot;CustPhone&quot;) = Request.Form(&quot;CustPhone&quot;)

rsCust.Update
rsCust.MoveLast
Else
Response.Redirect &quot;AddCust.asp?CustExists&quot;
End If

Response.Redirect &quot;CustAddSuccess.asp&quot;

'------------------------------------------------------------------------------
'--- Initialization
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'--- Body
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'--- Begin HTML output
'------------------------------------------------------------------------------
%>
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
</HEAD>

<BODY>


</BODY>
</HTML>
<%
'------------------------------------------------------------------------------
'--- End HTML Output
'------------------------------------------------------------------------------
rsCust.Close
Set rsCust = Nothing
cnCust.Close
Set cnCust = Nothing

'------------------------------------------------------------------------------
'--- All ASP post processing code goes here, as well as
'--- sub routines and functions
'------------------------------------------------------------------------------

%>
 
1st off, when submitting questions about DB links and what not, be very careful not to submit real users and password access to the DB. SA and *blank* are used too much!!!!!
If access to this DB is still sa and *blank* and has Net access, I would change it if I were you.


You probably want to do something like this....
1) connect to DB
2) check to see if record is there with....
Set rsCust = Server.CreateObject(&quot;ADODB.Recordset&quot;)
SQLstring=&quot;Select * from <TABLE> where <TABLE.CustCompanyName> = '&quot;&Request.Form(&quot;CustCompanyName&quot;)&&quot;' &quot;
rsCust.Open SQLstring, cnCust, adOpenStatic, adLockOptimistic


'This means if there is no record, put it in....
If rsCust.EOF
rsCust.addnew
'etc. etc. for all the fields.....
Else
redirect
'Redirect cuz record is already found......
End IF



Hope that helps
JP


_______________________________________________
OutsideIntranets.com
Stop wasting time, get to work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top