PDA

Click to See Complete Forum and Search --> : Help with IF-problem


shamloo
Oct 7th, 2000, 08:15 PM
Hi, I am making a kind-of admin-site for a site and haven't dealt with asp much before.
My problem:
First of all, this is how Im thinking:
------------------
*if the names from the form is NOT empty, do the following:
**if the first-name and the last-name from the form exists in the database Response.write:
That person is already added.
**if the names do NOT match the one in the database, add the person and his/her info in the database and response.write:
You have successfully added (the person)
**if the name-fileds were empty, response.write:
you forgot to fill in the name, please try again.
------------------
What's not working is if I try to add a person that already is in the database it creates a new
line in it with the info, and I do not want that to happen.

<!-- #include file="../db/adovbs.inc" -->
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<LINK REL="STYLESHEET" TYPE="text/css" HREF="rstr.css"></STYLE>
</HEAD>

<BODY BGCOLOR="#ffffff">
<TABLE BORDER="0" CELLPADDING="1" CELLSPACING="2">
<TR>
<TD CLASS="Heading" COLSPAN="7">
<%
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath("../db/roster.mdb")
Set RS = Server.CreateObject("ADODB.Recordset")
Addera = "SELECT * FROM teamROSTER"
RS.Open Addera, Connect, adOpenStatic, adLockOptimistic
Dim fNamn,eNamn,JerseyNr,Pos,height,weight,born,city

fNamn = Request.Form("fNamn")
eNamn = Request.Form("eNamn")
JerseyNr = Request.Form("Nr")
Pos = Request.Form("pos")
height = Request.Form("height")
weight = Request.Form("weight")
born = Request.Form("born")
city = Request.Form("city")

IF fNamn <>"" AND eNamn <>"" THEN
DO UNTIL RS.EOF
IF fNamn = RS("fNamn") AND eNamn = RS("eNamn") THEN
Response.write("<B>" & fNamn &" " & eNamn & "</B> is allready in the teamROSTER<BR></TD>")
EXIT DO
ELSEIF fNamn <> RS("fNamn") AND eNamn <> RS("eNamn") THEN
RS.AddNewRS("fNamn") = fNamn
RS("eNamn") = eNamn
RS("JerseyNr") = Jersey
NrRS("pos") = Pos
RS("Height") = Height
RS("Weight") = Weight
RS("Born") = Born
RS("Birthplace") = City
Response.write(RS("fNamn") & " " & RS("enamn") & " has been added to the teamROSTER<BR><BR></TD>")
EXIT DO
ELSE Response.write("")
END IF
RS.MoveNextLoop
ELSEIF fNamn = "" AND eNamn="" THEN
Response.write("You must fill in at least the players First and Last name<BR></TD>")
ELSE
Response.write("</TD>")
END IF
%>

</TR>
<TR>
<TD CLASS="TD1"><A HREF="teamROSTER.asp">View teamROSTER</A><BR></TD>
</TR>
</TABLE>
<%
RS.Update
RS.Close
Connect.Close
%>
</BODY></HTML>
This is the error-message I'm getting:
Error Type:
Provider (0x80020005)
Type mismatch.
/rstr/teamROSTER_added.asp, line 41

And that shows that one of the IF's does not work, it tries to add the person despite he/she already is in the db.
I'm getting that error-message cause I, on purpose, left a field blank.
Even though that field is empty that part of the code should NOT be executed.

does anyone of you see something wrong in the code above ??

I really need help with this, I've stared my self almost blind on this problem for hours...

thanks,
David

monte96
Oct 8th, 2000, 12:37 AM
For starters, you can't addnew to a static recordset. You will need to open it using either adOpenKeySet or adOpenDynamic.

Also, if you cut and pasted the code directly, look at your RS.AddNew line.. it has the next line on the same line. Same thing for your RS.MoveNext line. (If you typed it then it's probably just a typo- no big deal)

Make sure you are setting RS and Connect to Nothing at the bottom of your page.

Don't enclose your arguments for Response.Write in parenthesis unless it's returning something (which it's not)

This:

ELSEIF fNamn <> RS("fNamn") AND eNamn <> RS("eNamn") THEN
RS.AddNewRS("fNamn") = fNamn
RS("eNamn") = eNamn
RS("JerseyNr") = Jersey
NrRS("pos") = Pos
RS("Height") = Height
RS("Weight") = Weight
RS("Born") = Born
RS("Birthplace") = City
Response.write(RS("fNamn") & " " & RS("enamn") & " has been added to the teamROSTER<BR><BR></TD>")
EXIT DO
ELSE Response.write("")
END IF

Should just be this:

ELSE
RS.AddNew
RS("fNamn") = fNamn
RS("eNamn") = eNamn
RS("JerseyNr") = JerseyNr
RS("pos") = Pos
RS("Height") = Height
RS("Weight") = Weight
RS("Born") = Born
RS("Birthplace") = City
Response.write(RS("fNamn") & " " & RS("enamn") & " has been added to the teamROSTER<BR><BR></TD>")
EXIT DO
END IF


Since you are declaring your variables (which you should be), your object variables (Connect and RS) should also be declared, and while you don't HAVE to, you should declare them at the top rather than inside the code.

That's about all I can think of for now..