 |
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
|
|
|
|
|

April 11th, 2010, 06:20 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
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.
|
|

April 11th, 2010, 06:25 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

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

April 11th, 2010, 11:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

April 14th, 2010, 02:28 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

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

April 14th, 2010, 03:32 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Do you do some debugging before you post here?
Check your code: the Id is not as string literal....
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

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

April 14th, 2010, 03:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
 |