Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Producing decimal places when it shouldn;t be 8 42
Server Error 11 58
Facial recognition to look through the whole database for a person 3 61
MediaHelp 4 21
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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