Solved

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

Posted on 2014-09-19
3
244 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 32

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

10 Experts available now in Live!

Get 1:1 Help Now