Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2010, 06:20 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Add to cart button on Default.aspx

Hello sir,
I am trying to use the Add to cart button on the default.aspx page rather than ProductDetail.aspx
For this I had added a button on the default.aspx and created the handler for its click event as in the ProductDetail.aspx.vb page you have.

Code:
Protected Sub btnAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _productId As Integer
        _productId = Convert.ToInt32(Request.QueryString.Get("Id"))
        Dim myProduct As Product = ShopManager.GetProduct(_productId)
        ShopManager.AddProductToCart(myProduct)
        Response.Redirect("ShoppingCart.aspx")
    End Sub
I also changed the code in Default page that is I am trying to pass the Id of product on the same page on clicking of any hyperlink (Read More or Product title etc.).

Code:
<asp:HyperLink ID="hyperOrder" runat="server" NavigateUrl='<%# "Default.aspx?Id=" + Eval("Id").ToString() %>'>Read 
                More...</asp:HyperLink><br />
But the result is error something like this one.
object reference not set to an instance of an object.
Source Error:

Line 71: Public Sub Add(ByVal theProduct As Product)
Line 72: For Each existingProduct As OrderedProduct In _items
Line 73: If theProduct.Id = existingProduct.ProductId Then
Line 74: existingProduct.Quantity += 1
Line 75: Exit Sub


Sir was I correct while attempting to do so and if not then how it can be done that I may use the Add to cart button on the Default.aspx page.

Thank you.
 
Old April 11th, 2010, 06:25 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It looks like the page doesn't have an Id in the query string when you click Add. So this then:

_productId = Convert.ToInt32(Request.QueryString.Get("Id"))

results in an invalid product Id which causes this:

Dim myProduct As Product = ShopManager.GetProduct(_productId)

to not load a product which causes a null reference exception here:

If theProduct.Id = existingProduct.ProductId Then

What happens when you debug the Add to Cart code in Default.aspx? Do you see a valid ID in _productId?

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!
The Following User Says Thank You to Imar For This Useful Post:
sophia (April 11th, 2010)
 
Old April 11th, 2010, 10:33 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hello Sir,
as per your suggestion on debugging I got the value of _productId=0
and then the error that I face is

Code:
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 165:  Public Property SubTotal() As Decimal
Line 166:    Get
Line 167:      Return _theProduct.Price * _quantity
Line 168:    End Get
Line 169:    Set(ByVal value As Decimal)
sir how it can be removed as on the ProductDetail.aspx page it is working fine but on the Default.aspx it is doing so intended.
 
Old April 11th, 2010, 11:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
as per your suggestion on debugging I got the value of _productId=0
That explains it, doesn't it? You're trying to add a product to a cart that doesn't exist.

What makes you think you can have the Add to Cart button where you have it now? How should the button be hooked up to the product you want to add? In other words, what product are you trying to add to the cart?

It works on Detail.aspx becuase it is showing a product that you can add to the cart.

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!
 
Old April 14th, 2010, 01:52 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

<asp:button id="btnaddtocart" runat="server" text="add to cart" onclick="btnaddtocart_click" />
Can we pass the Id of product using Eval("Id") in the click event (btnaddtocart_click) so that now it knows the product and then assign it in the _productId in the code behind from within the button's click event rather than the query string.
If this can be done then the problem may solve.
Thank you.
 
Old April 14th, 2010, 02:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can Eval it to the CommandArgument propeprty of the button, and then retrieve it from the CommandArgument again in the button's Click event handler.

Untested, but something like this should work:

Code:
 
Dim productIdAsString As String = CType(sender, Button).CommandArgument
Hope this helps,

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!
 
Old April 14th, 2010, 03:10 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hello,
I added the command argument but as soon as I add this then no product appears in the browser. And as I remove it then the product appears again.

Code:
<asp:button id="btnaddtocart" runat="server" text="Add To Cart" onclick="btnaddtocart_click" CommandArgument="<%# Eval(Id)%>" />
what's wrong with it. why it is happening.
 
Old April 14th, 2010, 03:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Do you do some debugging before you post here?

Check your code: the Id is not as string literal....

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!
The Following User Says Thank You to Imar For This Useful Post:
sophia (April 14th, 2010)
 
Old April 14th, 2010, 03:47 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Really I must say you have eagle eye.
I made silly mistake, anyways thank you problem is solved now and in the code behind I used the following line for the button's click event

Code:
Dim productIdAsString As Integer = CType(sender, Button).CommandArgument
The original code was posted by you and I just modified it.
Once thank you, and for the next time I'll try not to make such silly mistakes.
 
Old April 14th, 2010, 03:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The CommandArgument is actually a string, so you really want this:

Dim productIdAsString As String = CType(sender, Button).CommandArgument

Then you have the ID as a string and convert it to an Integer:

Dim productId As Integer = Convert.ToInt32(productIdAsString)

Or do it all at once:

Dim productId As Integer = Convert.ToInt32(CType(sender, Button).CommandArgument)

In your implementation, you're relying on silent type conversion which may into always be
the best way to approach things.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add to cart problem phpcoder101 BOOK: Professional CodeIgniter ISBN: 978-0-470-28245-8 5 December 4th, 2009 07:00 AM
Code for Chpt.2-Wrox File Share is missing Default.aspx and Default.aspx.vb bzoni BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 October 23rd, 2009 02:39 PM
add to cart error haines BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 17th, 2008 05:49 PM
Make a image button as default submit button toshi ASP.NET 1.0 and 1.1 Basics 1 June 1st, 2006 05:25 AM
Add Push button or Check Box in outlook add-ins capdevs VS.NET 2002/2003 0 January 7th, 2006 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.