Solved

!String.IsNullOrEmpty(rawId) && int.TryParse(rawId, out productId)

Posted on 2014-09-19
3
250 Views
Last Modified: 2014-09-22
I have two pieces of code that I'm trying to get an explanation for:

1. if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
        {
          HttpContext.Current.Session[CartSessionKey] = HttpContext.Current.User.Identity.Name;
        }
        else

2.
string rawId = Request.QueryString["ProductID"];
      int productId;
      if (!String.IsNullOrEmpty(rawId) && int.TryParse(rawId, out productId))
      {
        using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
        {
          usersShoppingCart.AddToCart(Convert.ToInt16(rawId));
        }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using WingtipToys.Logic;

namespace WingtipToys
{
  public partial class AddToCart : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string rawId = Request.QueryString["ProductID"];
      int productId;
      if (!String.IsNullOrEmpty(rawId) && int.TryParse(rawId, out productId))
      {
        using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
        {
          usersShoppingCart.AddToCart(Convert.ToInt16(rawId));
        }

      }
      else
      {
        Debug.Fail("ERROR : We should never get to AddToCart.aspx without a ProductId.");
        throw new Exception("ERROR : It is illegal to load AddToCart.aspx without setting a ProductId.");
      }
      Response.Redirect("ShoppingCart.aspx");
    }
  }
}

Open in new window



 public string GetCartId()
    {
      if (HttpContext.Current.Session[CartSessionKey] == null)
      {
        if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
        {
          HttpContext.Current.Session[CartSessionKey] = HttpContext.Current.User.Identity.Name;
        }
        else
        {
          // Generate a new random GUID using System.Guid class.     
          Guid tempCartId = Guid.NewGuid();
          HttpContext.Current.Session[CartSessionKey] = tempCartId.ToString();
        }
      }
      return HttpContext.Current.Session[CartSessionKey].ToString();
    }

    public List<CartItem> GetCartItems()
    {
      ShoppingCartId = GetCartId();

      return _db.ShoppingCartItems.Where(
          c => c.CartId == ShoppingCartId).ToList();
    }
  }

Open in new window

0
Comment
Question by:yguyon28
3 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 167 total points
ID: 40332796
The first one checks to verifies that a valid user name has been entered (it has text, and is not just spaces) and then stores it in a session variable.

The second one is similar, in that it verifies that a valid integer value exists for rawID. (The first part of the if statement handles null values or and empty string, while the second part of the if statement verifies rawID can be converted to an integer.) If rawID is a valid integer, it will add it to the shopping cart.
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 167 total points
ID: 40332811
1 - is testing for the existence of the user who is currently connected to the application.  (If using windows or basic authentication it will return the user authenticated.)

2 - Go the the query string and get the productID.  

the query string should look something like:
http://mywebsite.com/mypage.aspx?ProductID=1234&SomeOtherParam=xyz

Note that the query string is case sensitive.  Also note that they should be checking for nulls.

int.tryParse will return a boolean value, true if the parse succeeds false if it doesn't.  (Imagine if someone put an "A" for the query string, and you productIDs are only numbers).

It then creates a shopping cart and adds the productID to the cart.
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 166 total points
ID: 40332825
if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
        {
          HttpContext.Current.Session[CartSessionKey] = HttpContext.Current.User.Identity.Name;
        }

Open in new window

Means that if HttpContext.Current.User.Identity.Name is not null, an empty string or an all whitespace string.  Then use it as the CartSettionKey in the current session.

string rawId = Request.QueryString["ProductID"];
      int productId;
      if (!String.IsNullOrEmpty(rawId) && int.TryParse(rawId, out productId))
      {
        using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
        {
          usersShoppingCart.AddToCart(Convert.ToInt16(rawId));
        }

Open in new window


Means that if rawId is not null or an empty string.  And it can be parsed as an integer (TryParse attempts to parse the value and returns true if it can be parsed as or false if it cannot be parsed as).  Then using a new ShoppingCartActions object, add the product id (as an integer) to the cart.

-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now