473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating A ShoppingCart

My problem is that everytime the DataRow tries to write in the DataTable a NullReferenceExeption occurs (Object reference not set to an instance of an object)

can anyone spot the problem

Expand|Select|Wrap|Line Numbers
  1.  DBConnection myConn = new DBConnection(); 
  2.  
  3. DateTime today = DateTime.Today;
  4. int bottleID;
  5.  
  6. DataTable cart = new DataTable("MyShoppingCart");
  7.  
  8. DataColumn DC1;
  9. DataColumn DC2;
  10. DataColumn DC3;
  11. DataColumn DC4;
  12.  
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. fillData();
  16.  
  17. if (Session["Cart"] == null)
  18. {
  19. CreateCart();
  20. DataTable cart = (DataTable)Session["Cart"];
  21. }
  22. else
  23. {
  24. DataTable cart = (DataTable)Session["Cart"];
  25. }
  26.  
  27.  
  28.  
  29. public void CreateCart()
  30. {
  31. DC1 = new DataColumn("BottleID", System.Type.GetType("System.Int32"));
  32. DC2 = new DataColumn("Quantity", System.Type.GetType("System.Int32"));
  33. DC3 = new DataColumn("Price", System.Type.GetType("System.Double"));
  34. DC4 = new DataColumn("Volume", System.Type.GetType("System.Int32"));
  35.  
  36. cart.Columns.Add(DC1);
  37. cart.Columns.Add(DC2);
  38. cart.Columns.Add(DC3);
  39. cart.Columns.Add(DC4);
  40.  
  41. cart = (DataTable)Session["Cart"];
  42.  
  43. }
  44.  
  45. public void fillData()
  46. {
  47. lblClientID.Text = myConn.UserLoggedin(Session["Username"].ToString());
  48. lblBottleID.Text = GetbottleInfo();
  49. lblDate.Text = today.ToString();
  50.  
  51.  
  52.  
  53.  
  54. protected void btnAddToCart_Click(object sender, EventArgs e)
  55. {
  56. DataTable cart = (DataTable)Session["Cart"];
  57.  
  58.  
  59.  
  60. DataRow DR = cart.NewRow();
  61.  
  62. DR[DC1] = bottleID;
  63. DR[DC2] = Convert.ToInt32(txtQty.Text);
  64. DR[DC3] = getBottlePrice(bottleID);
  65. DR[DC4] = cmbVol.SelectedValue;
  66.  
  67. cart.Rows.Add(DR); 
  68.  
  69. Response.Redirect("ShoppingCart.aspx");
  70. }
  71.  
May 15 '08 #1
0 738

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rdsteph | last post by:
Python411 is a series of podcasts about Python, aimed at hobbyists and others who are learning Python. Each episode focuses on one aspect of learning Python, or one kind of Python programming, and...
6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
2
by: Pawan | last post by:
Hi Guys, I have this current assignment where I have to develop online forms for local municipal authorities. I have to use adobe acrobat to create online forms from PDFs (which I have never done...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
0
by: Tarun Mistry | last post by:
Hi all, I'm trying to create a global object that can be used anywhere in my ASP.NET (2.0) application but im having problems. I would like to run some logic every time the page is loaded,...
1
oll3i
by: oll3i | last post by:
cd u please tell me if i shd make a shoppingcart a singleton
1
by: phantomlove | last post by:
i'm trying "E-commerce Starter Kit " at the downloads section (www.dotnetbips.com) .. when i click "Add to cart" button for the first time .. it works fine and added the product to the shopping cart...
9
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.