Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-19
3
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 501 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 501 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 34

Assisted Solution

by:it_saige
it_saige earned 498 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

660 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