<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Justice Solutions Web &#38; Graphics &#187; asp.NET</title>
	<atom:link href="http://justicesolutionsllc.com/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://justicesolutionsllc.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 02:38:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.NET Shopping Cart Example Using Only Session Variables</title>
		<link>http://justicesolutionsllc.com/net-shopping-cart-example-using-only-session-variables/</link>
		<comments>http://justicesolutionsllc.com/net-shopping-cart-example-using-only-session-variables/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 18:27:51 +0000</pubDate>
		<dc:creator>Doug</dc:creator>
				<category><![CDATA[asp.NET]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Shopping Carts]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Web Programming]]></category>

		<guid isPermaLink="false">http://www.justicesolutionsllc.com/?p=122</guid>
		<description><![CDATA[Shopping carts are basically a dime a dozen.  But there&#8217;s always some component of them that doesn&#8217;t quite fit into your current model.  So what do you do when you are up against a deadline and need to quickly produce a basic front end of a shopping cart to hold items and quantities in .NET [...]]]></description>
			<content:encoded><![CDATA[<p>Shopping carts are basically a dime a dozen.  But there&#8217;s always some component of them that doesn&#8217;t quite fit into your current model.  So what do you do when you are up against a deadline and need to quickly produce a basic front end of a shopping cart to hold items and quantities in .NET using on Session Variables?</p>
<p>I came up with this solution after thinking about a quick way to avoid having to read and write to a database, but not storing elaborate arrays and such to try and accomplish a very simple shopping cart structure.</p>
<p><code><br />
// Get the Item We're Going to Add to the Cart<br />
int itemID = Convert.ToInt32(Request.QueryString["itemid"]);</p>
<p>// Get the Item (I use a Business Logic here, but you could easily grab the item information from your //database call or items from a grid, etc.<br />
item m = new item();<br />
itemAssembler ma = new itemAssembler();<br />
m = ma.ReadSingleByKey(itemID);</p>
<p>// Now grab a random number so we can track the items in the cart individually</p>
<p>Random rand = new Random();<br />
int x;<br />
x = rand.Next(100000, 999999);</p>
<p>// Check and see....Do we have a cart already?<br />
if (Session["cart"] == null)<br />
{</p>
<p>// Add the item to the cart<br />
NameValueConfigurationCollection cart = new NameValueConfigurationCollection();</p>
<p>//Add an Item to the cart ("ITEM",Variance, ItemID,Quantity)<br />
NameValueConfigurationElement nvc = new NameValueConfigurationElement("ITEM," + x.ToString() + "," + "1", m.id.ToString());<br />
cart.Add(nvc);<br />
Session["cart"] = cart;<br />
}<br />
else<br />
{<br />
NameValueConfigurationCollection cart = Session["cart"] as NameValueConfigurationCollection;</p>
<p>//Add an Item to the cart ("ITEM",ItemID,Quantity)<br />
NameValueConfigurationElement nvc2 = new NameValueConfigurationElement("ITEM," + x.ToString() + "," + "1", m.id.ToString());<br />
cart.Add(nvc2);<br />
Session["cart"] = cart;<br />
}</p>
<p>// Now take your user to the cart view page you'd create<br />
Response.Redirect("your_cart.aspx");<br />
</code></p>
<p>
Now on our &#8220;your_cart&#8221; page, you can now loop through the items in the cart and display them however you&#8217;d like.
</p>
<p><code><br />
// Let's load the items from the shopping cart into the table<br />
            NameValueConfigurationCollection cart = Session["cart"] as NameValueConfigurationCollection;<br />
            foreach (NameValueConfigurationElement n in cart)<br />
            {</p>
<p>                //Now split the value for the Item Type, the ID, and the Quantity<br />
                String typelist = n.Name;<br />
                char[] sep = { ' ', ',', '.', ':', 't' };<br />
                String[] typeitems = typelist.Split(sep);</p>
<p>                //Hold these values in some String Variables<br />
                String type = typeitems[0];<br />
                String cartitem = typeitems[1];<br />
                String qty = typeitems[2];</p>
<p>                //Now set up our currency display<br />
                System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();<br />
                nfi.CurrencyDecimalDigits = 2;<br />
                nfi.CurrencySymbol = "$";</p>
<p>                //Take your business/data logic and grab the information<br />
                item i = new item();<br />
                itemAssembler ia = new itemAssembler();<br />
                itemCriteria ic = new itemCriteria();</p>
<p>                if(ia.ReadSingleByKey(Convert.ToInt32(n.Value)) != null)<br />
                {<br />
                i = ia.ReadSingleByKey(Convert.ToInt32(n.Value));</p>
<p>                // Get the Retail Price<br />
                int productid = i.id;<br />
                pricing pps = new pricing();<br />
                decimal retailprices = Convert.ToDecimal(i.retail);// +Convert.ToDecimal(i.shipping);</p>
<p>                //Now take the Table you have on your display page and add rows to it...<br />
                HtmlTableRow mainrow = new HtmlTableRow();<br />
                HtmlTableCell maincell = new HtmlTableCell();<br />
                maincell.InnerHtml = "<br />
<tr>
<td align='center' bgcolor='gainsboro' colspan='3' style='text-align: left' valign='top'>" + i.name + "</td>
<td bgcolor='#CCCC99'><a href='RemoveItem.aspx?cartitem=" + n.Name + "'>Remove Item</a></td>
<p>";<br />
                mainrow.Cells.Add(maincell);<br />
                tblCase.Rows.Add(mainrow);</p>
<p>            }</p>
<p>            System.Globalization.NumberFormatInfo tnfi = new System.Globalization.NumberFormatInfo();<br />
            tnfi.CurrencyDecimalDigits = 2;<br />
            tnfi.CurrencySymbol = "$";</p>
<p>            // Now populate the items on the Total Costs Table<br />
            lblSubtotal.Text = String.Format(tnfi, "{0:c}", subtotal);<br />
            lblTax.Text = String.Format(tnfi, "{0:c}", tax);<br />
            lblShipping.Text = String.Format(tnfi, "{0:c}", shipping);<br />
            total = subtotal + shipping + tax;<br />
            lblTotal.Text = String.Format(tnfi, "{0:c}", total);<br />
        }</p>
<p></code></p>
<p>
You now have a simple shopping cart in just a few lines of code since most of the code is used to get the information about your particular item and can be streamlined even more if you wanted to store all of that into the string as well.  Ok, so now that you have a way to put together a quick cart for your client before morning&#8230;.happy coding</p>
]]></content:encoded>
			<wfw:commentRss>http://justicesolutionsllc.com/net-shopping-cart-example-using-only-session-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

