Forum Moderators: open

Message Too Old, No Replies

how to prevent duplicate record

         

vanjamier

4:33 am on Feb 17, 2005 (gmt 0)

10+ Year Member



Hi there,

I need help on how to prevent the application from inserting an already exisiting record.
I tried on something however it doesnt work..
I do hope can get solution, thanks alot.


<!-- #include file="include/connections/database.inc" -->
<%
If Request.Form("submit")="Submit" Then
Set rs = Server.CreateObject("ADODB.RecordSet")
Set rs2 = Server.CreateObject("ADODB.RecordSet")

strName = Request.Form("txtname")
strloginId = Request.Form("loginId")
strlogin = Request.Form("login") 'combobox
strdepartment = Request.Form("department")
strcenter = Request.Form("center")
strnumber = Request.Form("number")
strmodel = Request.Form("model")
strimei = Request.Form("imei")
strwarranty = Request.Form("warranty")
strdate1 = Request.Form("date1")
strdate2 = Request.Form("date2")
straccount = Request.Form("account") 'combo box
strplan = Request.Form("plan") 'combo box
strdate3 = Request.Form("date3")
strdate4 = Request.Form("date4")
strdate5 = Request.Form("date5")
strmline = Request.Form("mline")
strremarks = Request.Form("remarks")

'open the database from database.inc file
OpenDatabase()

strsql2 = "SELECT NAME,LOGINID from sunder.mobile_users where NAME = '" & strName & "' and LOGINID ='" & strloginId & "' "
rs2.Open strsql2, conn

If rs2.recordcount > 0 Then

Response.Write "<br><font color=""red"">Same name..."
Response.Write "</font>"
Response.Write "<br>"
Response.Write "<A href=""admin_view.asp"">Click here to try again..."

Else
strsql = "INSERT INTO sunder.mobile_users(DEPT, COST_CENTRE, NAME, PHONE_NO, MODEL, IMEI, DATE_OF_PURCHASE, REMARKS, WARRANTY_CARD, HP_DATE_OF_PURCHASE, SINGTEL_ACCOUNT_NO, PLAN_TYPE, SINGTEL_ONE_YR, ELECTRICLINK_TWO_YR, SINGTEL_TWO_YR, MOBILE_LINE, LOGINID, LOGINTYPE) " &_
"VALUES ('" & strdepartment & "', '" & strcenter & "', '" & strName & "', '" & strnumber & "', '" & strmodel & "', '" & strimei & "', '" & strdate1 & "', '" & strremarks & "', '" & strwarranty & "', '" & strdate2 & "', '" & straccount & "', '" & strplan & "', '" & strdate3 & "', '" & strdate4 & "', '" & strdate5 & "', '" & strmline & "', '" & strloginId & "', '" & strlogin & "')"

rs.Open "sunder.mobile_users", conn, 2, 2

'Adding data
rs.AddNew
rs("DEPT") = strdepartment
rs("COST_CENTRE") = strcenter
rs("NAME")= strName
rs("PHONE_NO") = strnumber
rs("MODEL") = strmodel
rs("IMEI") = strimei
if strdate1 <> "" then rs("DATE_OF_PURCHASE") = strdate1
rs("REMARKS") = strremarks
rs("WARRANTY_CARD") = strwarranty
if strdate2 <> "" then rs("HP_DATE_OF_PURCHASE") = strdate2
rs("SINGTEL_ACCOUNT_NO") = straccount
rs("PLAN_TYPE") = strplan
if strdate3 <> "" then rs("SINGTEL_ONE_YR") = strdate3
if strdate4 <> "" then rs("ELECTRICLINK_TWO_YR") = strdate4
if strdate5 <> "" then rs("SINGTEL_TWO_YR") = strdate5
if strmline <> "" then rs("MOBILE_LINE") = strmline
rs("LOGINID") = strloginId
rs("LOGINTYPE") = strlogin
rs.Update

rs.close

Response.Redirect("admin_view.asp")
End If
rs2.close
End If
set rs = nothing
set rs2 = nothing
%>


Thanks for the attention, cheers

Easy_Coder

1:36 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way is to enforce your primary key. Too, you should do an existance check before writing data into the table...

if not exists(select this from that where field = value etc.....)
begin
-- do stuff here because i'm a unique record....
end