Select Max Value And Enter Into DB - URGENT 500 POINTS

Can anyone see a problem with this code:

<%
intCartID = Session("CartID")

IF intCartID = 0 THEN
      
      Set RS = Server.CreateObject("ADODB.Recordset")
      SQL = "SELECT MAX(intCartID) AS intCartID1 FROM tblCarts"
      RS.Open SQL,Connection,3,3
      
      intLastCartID = intCartID1
      intNewCartID = intLastCartID + 1
            
      RS.AddNew
            RS.Fields("txtCartStatus") = "OPEN"
            RS.Fields("intCartID") = intNewCartID
      RS.Update
      
      RS.Close
      Set RS = Nothing

ELSE
      
      Response.Write "CartID = " & intCartID
      
END IF
%>
net-workxAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
If this:

     intLastCartID = intCartID1
     intNewCartID = intLastCartID + 1

Is supposed to be reading the value from your SQL query then it should say:

     intLastCartID = RS("intCartID1")
     intNewCartID = intLastCartID + 1
0
 
WMIFCommented:
what is your problem.  is it not working?  are you getting an error?
0
 
net-workxAuthor Commented:
sorrry! cardinal sin, i forgot the error...

ADODB.Recordset (0x800A0CC1)
Item cannot be found in the collection corresponding to the requested name or ordinal.
/includes/cart/processing/add_to_cart.asp, line 17


bascially is should be selecting the max value from a column called intCartID in a table called tblCarts

Once i have established what the max value in that column is i need to put it into a variable, add 1 to it and put that into another variable

This second variable then needs to be entered into the DB and into the column intCartID

the RS.Fields("txtCartStatus") = "OPEN" does not matter at this point, ill sort that out later...

Cheers,
Carl
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
net-workxAuthor Commented:
This code runs and adds a record into the DB but each record has an intCartID of 1

<%
intCartID = Session("CartID")

IF intCartID = 0 THEN
      
      Set RS = Server.CreateObject("ADODB.Recordset")
      SQL = "SELECT MAX(intCartID) AS intCartID1 FROM tblCarts"
      RS.Open SQL,Connection,3,3
      
      intLastCartID = intCartID1
      intNewCartID = intLastCartID + 1
      
      RS.Close
      Set RS = Nothing
      
      Set RS = Server.CreateObject("ADODB.Recordset")
      SQL = "SELECT * FROM tblCarts WHERE intCartID = 0"
      RS.Open SQL,Connection,3,3
      
      RS.AddNew
            RS.Fields("txtCartStatus") = "OPEN"
            RS.Fields("intCartID") = intNewCartID
      RS.Update
      
      RS.Close
      Set RS = Nothing

ELSE
      
      Response.Write "CartID = " & intCartID
      
END IF
%>

I then changed it to:

<%
intCartID = Session("CartID")

IF intCartID = 0 THEN
      
      Set RS = Server.CreateObject("ADODB.Recordset")
      SQL = "SELECT MAX(intCartID) AS intCartID1 FROM tblCarts"
      RS.Open SQL,Connection,3,3
      
      intLastCartID = RS("intCartID1")
      intNewCartID = intLastCartID + 1
      
      RS.Close
      Set RS = Nothing
      
      Set RS = Server.CreateObject("ADODB.Recordset")
      SQL = "SELECT * FROM tblCarts WHERE intCartID = 0"
      RS.Open SQL,Connection,3,3
      
      RS.AddNew
            RS.Fields("txtCartStatus") = "OPEN"
            RS.Fields("intCartID") = intNewCartID
      RS.Update
      
      RS.Close
      Set RS = Nothing

ELSE
      
      Response.Write "CartID = " & intCartID
      
END IF
%>

and it works ok so i was needing to create a new recordset for some reason and then i entered the fix that carl_tawn suggested.

Any improvements then please let me know.

Points are awarded to carl_tawn

Cheers,
Carl
0
 
Carl TawnSystems and Integration DeveloperCommented:
The reason that you needed to open a second recordset is because the original created from "SELECT MAX(intCartID) AS intCartID1 FROM tblCarts" only contains a single value. It doesn't actually contain the columns "intCartStatus" and "intCartID" that you were trying to write to after calling AddNew.

So, as you have done, you needed to retireve the full record so that the columns would then be available for writing your values to.
0
 
net-workxAuthor Commented:
Top Stuff,

Thank you for the explaination and clarification.

Regards,
Carl
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.