Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8
This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 by Chris Hart, John Kauffman, David Sussman, Chris Ullman; ISBN: 9780764588501
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 2nd, 2006, 04:57 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 13 Shopping Cart

In Checkout.aspx the line below

OrderID = Convert.ToInt32(cmd.ExecuteScalar())

OrderID is always 0 when inserted into table OrderLines

Can anyone please advise.

thanks,
Michael.



Reply With Quote
  #2 (permalink)  
Old September 11th, 2006, 01:41 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Might be because you haven't debugged and there is an error on p.479 - Evel should have brackets around.i.e. Eval("ProductID", "WroxShopItem.aspx?ProductID={0}")

Reply With Quote
  #3 (permalink)  
Old October 5th, 2006, 05:43 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, I finally found the answer if you are still interested:

you add this to the end of the first INSERT SQL statement: & _
        "SELECT @@IDENTITY AS OrderID;" and it works fine

Reply With Quote
  #4 (permalink)  
Old October 13th, 2006, 09:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Banbury, , United Kingdom.
Posts: 105
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to DanM
Default

michaelcode,

As noted in this thread - http://p2p.wrox.com/topic.asp?TOPIC_ID=40977 - you might also want to try using

'SELECT SCOPE_IDENTITY() AS OrderID'

as well.

dan

Dan Maharry
Read the feed at http://feeds.feedburner.com/dansarchive
Reply With Quote
  #5 (permalink)  
Old May 24th, 2007, 06:57 PM
Registered User
 
Join Date: May 2007
Location: Beijing, , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I'm using VWD 2005 Express with C#.
cmd.CommandText = "INSERT INTO Orders(MemberName,OrderDate,Name,Address,County,Po stCode,Country,Total)" +
                "VALUES (@MemberName,@OrderDate,@Name,@Address,@County,@Po stCode,@Country,@Total)"+"SELECT @@IDENTITY AS OrderID";
It is OK now.

Reply With Quote
  #6 (permalink)  
Old January 24th, 2010, 04:06 PM
Registered User
 
Join Date: Jan 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default repeat always firts product (VB)

Hello, I'm trying to adapt the code for the shopping cart to a database in Access instead of SQL Express.

the code is as follows :
Code:
 
Dim OrderID As Integer
Dim strConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database1.mdb")
Dim mySql As String = "INSERT INTO Orders(OrderDate, Company, Contact, Email, Phone, Address, City, PostCode, Country, Shipment, Notes) " & _
           "VALUES (Now(), @Company, @Contact, @Email, @Phone, @Address, @City, @PostCode, @Country, @Shipment, @Notes)"
   
        Dim cmd As New OleDbCommand(mySql, strConn)
        
        Try
            strConn.Open()

            'cmd.Parameters.AddWithValue("MemberName", User.Identity.Name)
            cmd.Parameters.AddWithValue("Company", txtCompany.Text)
            cmd.Parameters.AddWithValue("Contact", txtContact.Text)
            cmd.Parameters.AddWithValue("Email", txtEmail.Text)
            cmd.Parameters.AddWithValue("Phone", txtPhone.Text)
            cmd.Parameters.AddWithValue("Address", txtAddress.Text)
            cmd.Parameters.AddWithValue("City", txtCountry.Text)
            cmd.Parameters.AddWithValue("PostCode", txtPostCode.Text)
            cmd.Parameters.AddWithValue("Country", txtCountry.Text)
            cmd.Parameters.AddWithValue("Shipment", txtShipment.Text)
            cmd.Parameters.AddWithValue("Notes", txtNote.Text)
         
            cmd.ExecuteNonQuery()    'This executes the first command
            cmd.CommandText = "SELECT @@Identity As OrderID"
            
            OrderID = cmd.ExecuteScalar() 'This Retrieves the OrderID that was created 
           
            cmd.CommandText = "INSERT INTO OrderLines(OrderID, ProductID, ProductName, Quantity, Measure, Package) " & _
             "VALUES (@OrderID, @ProductID, @ProductName, @Quantity, @Measure, @Package)"
            
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@OrderID", OrderID)
           
            For Each item As CartItem In Profile.Cart.Items
           
                cmd.Parameters.AddWithValue("@ProductID", item.ProductID)
                cmd.Parameters.AddWithValue("@ProductName", item.ProductName)
                cmd.Parameters.AddWithValue("@Quantity", item.Quantity)
                cmd.Parameters.AddWithValue("@Measure", item.Measure)
                cmd.Parameters.AddWithValue("@Package", item.Package)
                
                cmd.ExecuteNonQuery()
            Next
          
            ' clear the cart
            Profile.Cart.Items.Clear()


        Catch ex As Exception
          
            Throw

        Finally
            strConn.Close()

        End Try

work pretty well, the problem is that if I put in the cart (for example) 5 products, he writes in the database 5 times the first product.

What am I doing wrong?
Reply With Quote
  #7 (permalink)  
Old January 24th, 2010, 04:50 PM
Imar's Avatar
Wrox Author
Points: 67,271, Level: 100
Points: 67,271, Level: 100 Points: 67,271, Level: 100 Points: 67,271, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,227
Thanks: 66
Thanked 1,413 Times in 1,393 Posts
Default

Hi there,

Try calling cmd.Parameters.Clear() as the first line in your for loop and see of that makes a difference.

Chers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #8 (permalink)  
Old January 25th, 2010, 02:27 AM
Registered User
 
Join Date: Jan 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks

I tried, but I get the error "OleDBException. No value specified for some required parameters."
Reply With Quote
  #9 (permalink)  
Old January 25th, 2010, 02:32 AM
Imar's Avatar
Wrox Author
Points: 67,271, Level: 100
Points: 67,271, Level: 100 Points: 67,271, Level: 100 Points: 67,271, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,227
Thanks: 66
Thanked 1,413 Times in 1,393 Posts
Default

In that case, take a look here:

http://social.msdn.microsoft.com/for...5-fc726bb62e09

Create the parameters outside the loop, and then set their value using cmd.Parameters.Item(0).Value inside the loop.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #10 (permalink)  
Old January 25th, 2010, 02:50 AM
Registered User
 
Join Date: Jan 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I modified code as follows :

Code:
  For Each item As CartItem In Profile.Cart.Items
                cmd.Parameters.Clear()
                
                cmd.CommandText = "INSERT INTO OrderLines(OrderID, ProductID, ProductName, Quantity, Measure, Package) " & _
             "VALUES (@OrderID, @ProductID, @ProductName, @Quantity, @Measure, @Package)"
     
                cmd.Parameters.AddWithValue("@OrderID", OrderID)
                cmd.Parameters.AddWithValue("@ProductID", item.ProductID)
                cmd.Parameters.AddWithValue("@ProductName", item.ProductName)
                cmd.Parameters.AddWithValue("@Quantity", item.Quantity)
                cmd.Parameters.AddWithValue("@Measure", item.Measure)
                cmd.Parameters.AddWithValue("@Package", item.Package)
                
                cmd.ExecuteNonQuery()
            Next
and seems to work

thanks anyway for the help
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13: shopping cart problem mysecondlove BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 April 9th, 2008 06:48 PM
Chapter 13 Shopping Cart disappears after login bwinklesky BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 June 14th, 2007 12:34 PM
Ch.13 Builing a Shopping Cart seannie BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 3 February 7th, 2007 09:11 AM
Shopping Cart Script in Chapter 15 groovybaz PHP How-To 4 June 7th, 2006 08:21 AM



All times are GMT -4. The time now is 08:01 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.