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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WMIFCommented:
what is your problem.  is it not working?  are you getting an error?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
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.
net-workxAuthor Commented:
Top Stuff,

Thank you for the explaination and clarification.

Regards,
Carl
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.