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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nightmarish wish list code 1

Status
Not open for further replies.

loveday

Programmer
Sep 19, 2004
106
US
I have been battling this wish list code for only God knows how long but just can't seem to get over the hump.

First I searched the forum and found wish list functions and subs with dlls by Cassidy.

I just couldn't much with it.

Then gradually, I started putting the pieces together but somehow, just can't seem to cross over the huddle.

Right now, the Add to wish list functionality is working, items are getting inserted into the wish list table.

However, The items are not getting displayed on the screen even the Select * from wishlist table seems fine to me.

The second problem I am having is the delete from Wishlist code.

I keep getting an error that says:

Error Type:
Microsoft JET Database Engine (0x80040E0C)
Command text was not set for the command object.
/WishList.asp, line 28


I have attached the entire code below.

Please, please help... it is really getting desparate now.

It is probably something real simple...but I can't see it.


thanks much in advance

Code:
<HTML><BODY>
<%
IF session("custid") = "" Then 'We have no value in session

  response.redirect "error.asp?msg=" & server.urlencode("some services require you to login or register")

End IF

Set objConn=Server.CreateObject("ADODB.Connection")
objConn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
             "DATA SOURCE=" & server.mappath("admin\scart.mdb")

' Did user ask to add any items to the wishlist?
If Request("AddToWishList") <> "" Then

    items = Request("AddItem")
    If Trim(items) = "" Then
       Response.Write "No products given to us to add!!<P>Doing nothing."
       Response.End
   End If

        items = Split( items, ", " ) ' comma-space
        For inum = 0 To UBound(items)
    SQL = "INSERT INTO WishList (idCustomer, idProduct) " _
         & " VALUES(" & Session("custID") & "," & items(inum) & ")"
Next
End If
Set rs = objConn.Execute( SQL )
'response.write SQL
'response.end


' Did user ask to delete any items from the wishlist?
If Request("DeleteFromWishList") <> "" Then
    ' yep
    items = Request("RemoveItem")
    If Trim(items) = "" Then
       Response.Write "No products given to us to add!!<P>Doing nothing."
       Response.End
    End If

  'Now let's remove an item or items from the wishlist
   SQL = "DELETE FROM wishlist " _
              & " WHERE idCustomer=" & Session("custID") & " " _
              & " AND idProduct IN ('" & Replace( items, ", ", "','" ) & "')"
   Set rs = objConn.Execute( SQL )
   Response.Write "Removing: " & SQL & "<br/>"
End If ' end of if user asked to remove items

' now show current state of wishlist:
' In this simulation, we can only show the product ID of each...
' in the real thing, the SQL would of course show all relevant info
Response.Write "<HR>"

SQL = "SELECT * FROM wishlist WHERE idCustomer = " & Session("custID") & " "
Set rs = objConn.Execute( SQL )

Response.Write "Getting wishlist: " & SQL & "<HR>"
%>
<FORM>
<TABLE Border=1 CellPadding=5>
<TR>
   <TH>Remove<br/>from list</TH>
   <TH>Product ID</TH>
   <TH>Description</TH>
</TR>

<%
Do While Not rs.EOF
%>
        <TR>
        <TD><INPUT Type=Checkbox Name="RemoveItem" Value="<%=item%>"></TD>
        <TD><%=item%></TD>
        <TD>&nbsp;</TD>
        </TR>
<%
    rs.MoveNext
Loop
rs.Close
objConn.Close
%>
<TR>
    <TD Colspan=3>&nbsp;</TD>
</TR>
<TR>
    <TD>
    <INPUT Type=Submit Name="DeleteFromWishlist"
           Value="Remove checked items to wishlist">
    </TD>
    <TD>&nbsp;</TD>
    <TD align=right>
    <INPUT Type=Submit Value="Show the list of products"
           onClick="this.form.action='productChoices.asp'; return true;">
    </TD>
</TR>
</TABLE>
</FORM>
</BODY></HTML>
 
You don't have to do this but its always good to seperate each section of code somehow, for someone else like myself it was hard to seperate... I usually use something like this to do so:


'####################################################
'# Start of selecting wish list
'####################################################

The problem I believe is the Recordset was not open. See bolded


Code:
<HTML><BODY>
<%
IF session("custid") = "" Then 'We have no value in session

  response.redirect "error.asp?msg=" & server.urlencode("some services require you to login or register")

End IF


'####################################################
'# Start inserting selected wish list
'####################################################

[highlight]Set rs = Server.CreateObject("ADODB.Recordset")[/highlight]
Set objConn=Server.CreateObject("ADODB.Connection")
objConn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
             "DATA SOURCE=" & server.mappath("admin\scart.mdb")

' Did user ask to add any items to the wishlist?
If Request("AddToWishList") <> "" Then

    items = Request("AddItem")
    If Trim(items) = "" Then
       Response.Write "No products given to us to add!!<P>Doing nothing."
       Response.End
   End If

        items = Split( items, ", " ) ' comma-space
        For inum = 0 To UBound(items)
    SQL = "INSERT INTO WishList (idCustomer, idProduct) " _
         & " VALUES(" & Session("custID") & "," & items(inum) & ")"
Next
End If
Set rs = objConn.Execute( SQL )
'response.write SQL
'response.end


'####################################################
'# Start deleting selected wish
'####################################################

' Did user ask to delete any items from the wishlist?
If Request("DeleteFromWishList") <> "" Then
    ' yep
    items = Request("RemoveItem")
    If Trim(items) = "" Then
       Response.Write "No products given to us to add!!<P>Doing nothing."
       Response.End
    End If

  'Now let's remove an item or items from the wishlist
   SQL = "DELETE FROM wishlist " _
              & " WHERE idCustomer=" & Session("custID") & " " _
              & " AND idProduct IN ('" & Replace( items, ", ", "','" ) & "')"
   Set rs = objConn.Execute( SQL )
   Response.Write "Removing: " & SQL & "<br/>"
End If ' end of if user asked to remove items

' now show current state of wishlist:
' In this simulation, we can only show the product ID of each...
' in the real thing, the SQL would of course show all relevant info
Response.Write "<HR>"


'####################################################
'# Start selecting wish list
'####################################################

SQL = "SELECT * FROM wishlist WHERE idCustomer = " & Session("custID") & " "
Set rs = objConn.Execute( SQL )

Response.Write "Getting wishlist: " & SQL & "<HR>"
%>
<FORM>
<TABLE Border=1 CellPadding=5>
<TR>
   <TH>Remove<br/>from list</TH>
   <TH>Product ID</TH>
   <TH>Description</TH>
</TR>

<%
Do While Not rs.EOF
%>
        <TR>
        <TD><INPUT Type=Checkbox Name="RemoveItem" Value="<%=item%>"></TD>
        <TD><%=item%></TD>
        <TD>&nbsp;</TD>
        </TR>
<%
    rs.MoveNext
Loop
rs.Close
objConn.Close
%>
<TR>
    <TD Colspan=3>&nbsp;</TD>
</TR>
<TR>
    <TD>
    <INPUT Type=Submit Name="DeleteFromWishlist"
           Value="Remove checked items to wishlist">
    </TD>
    <TD>&nbsp;</TD>
    <TD align=right>
    <INPUT Type=Submit Value="Show the list of products"
           onClick="this.form.action='productChoices.asp'; return true;">
    </TD>
</TR>
</TABLE>
</FORM>
</BODY></HTML>

www.sitesd.com
ASP WEB DEVELOPMENT
 
One problem with both your insert and delete is that neither type of SQL is supposed to return recordsets. As such in both places there where you have:
Code:
set rs = objConn.Execute( SQL )
you just need
Code:
objConn.Execute( SQL )
Also, when using the execute method rather than the open with a select, you do not need to ad the line suggested above: the object is instantiated automatically. Adding that line will just instantiate an object needlessly, as it will be replaced by the new one instantiated by the execute.

One major problem I see with the display code is that "item" isn't a thing -- it's just an empty variable at this point in the code. You probably want something like
Code:
rs("item")
instead (using the actual database field name inside the quotes).

Have you gone through the ADO tutorial at at this point? It doesn't take too long and does a good job of explaining some of the fundamentals you might not be aware of.
 
Thanks folks for your feedback.

I am still getting same error and I am still not getting anything displayed on screen.
 
After your
Code:
   SQL = "DELETE FROM wishlist " _
              & " WHERE idCustomer=" & Session("custID") & " " _
              & " AND idProduct IN ('" & Replace( items, ", ", "','" ) & "')"
add in
Code:
Response.Write("<h3>" & SQL & "</h3>")
Response.Flush
to see precisely what the database is trying to execute. It's likely that there's a problem with either "items" or your session variable.
 
the error is occuring on line 34 which is here:


Set rs = objConn.Execute(SQL)

Although after reading your first post, I changed that line to this line:

objConn.Execute(SQL)

This error is occurring in the insert statement block.

What is funny is that records are being inserted into the database.

So the code you just asked me to insert after the delete statement, would not have processed anyway because the error on line 34 is not letting the delete block to process.


 
Well, let's start by fixing your code and we'll see if we can't narrow down the error.

The logic I see says to me that if the user clicked the Add to Wish List button, you want to run through the list of items and insert them into the datbase. Here's that chunk of code as you have it now:
Code:
If Request("AddToWishList") <> "" Then

    items = Request("AddItem")
    If Trim(items) = "" Then
       Response.Write "No products given to us to add!!<P>Doing nothing."
       Response.End
   End If

        items = Split( items, ", " ) ' comma-space
        For inum = 0 To UBound(items)
    SQL = "INSERT INTO WishList (idCustomer, idProduct) " _
         & " VALUES(" & Session("custID") & "," & items(inum) & ")"
Next
End If
Set rs = objConn.Execute( SQL )
To accomplish the code of inserting every item you're looping through your array of items. Inside the loop you're setting the variable SQL to some sql code. However, if you look, that's all you're doing -- the Next command tells it to set the variable to the next item in the list, over and over, until it's gone through the whole array, at which point SQL contains sql for inserting the last item in the array. Then you exit the if and then execute the SQL.

This doesn't make any sense. First, you don't want to execute the sql outside of your If statement, because if the user didn't click Add, it's going to try to execute a blank SQL string (which will result in an error). Also, you want to execute all of the inserts, not just the last one, so the execute statement also needs to be inside the For-Next loop. Plus, as I mentioned above, you don't want to "set rs" to anything here, you only want to do that with a Select statement (most of the time). So let's replace that existing bit of code with this, my changes highlighted in red:
Code:
If Request("AddToWishList") <> "" Then
   items = Request("AddItem")
   If Trim(items) = "" Then
      Response.Write "No products given to us to add!!<P>Doing nothing."
      Response.End
   End If
   items = Split( items, ", " ) ' comma-space
   For inum = 0 To UBound(items)
      SQL = "INSERT INTO WishList (idCustomer, idProduct) " _
      & " VALUES(" & Session("custID") & "," & items(inum) & ")"
      [COLOR=blue]objConn.Execute( SQL )[/color]
   Next
End If
[COLOR=red][s]Set rs = objConn.Execute( SQL )[/s][/color]
Here we've moved the modifeid execute to inside the loop, and gotten rid of the recordset (which you don't need). Replace that code and let's see what error you've got.

(PS: when you tell us what line number you're having a problem with, it would help if you'd tell us which line that is, along with the precise error.)
 
Thanks again for all the help.

That error has disappeared.

However, I got my custom message on the delete block that says:

No products given to us to delete!!<P>Doing nothing

BTW:

On the delete block, I put this message as:

No products given to us to add!!<P>Doing nothing

I have since corrected it to read as indicated above.

The reason I am getting that error is because nothing is getting displayed on the screen.

Even though records are being added to the database, my select statement is not displaying them on the screen.

Once they are displayed, I have a checkbox that reads "Removed Checked items to wish list"

So no data is being displayed, letting me know that somethign is wrong with select statement block.

Then even though screen is blank, if you check a box to delete, you get the message I just posted.
 
Comment out the "Response.End" in your delete section and let's see what you get.
 
i have got it to work.

The only problem, besides the insert you fixed, is just adding this line of code just below the while loop:

product = rs("idPrudct")

Thanks for the great help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top