hi friends
i have a shopping cart that has a button for add to cart;
here , there are my codes for click event of the button:
protected void AddToCart(object sender, ImageClickEventArgs e) { UserControl cart = (UserControl)Master.FindControl("cart1"); DataGrid mydg = (DataGrid)cart.FindControl("dg"); Label lbl = (Label)cart.FindControl("lblTotal");
product = productName; DataTable objDT = (DataTable)Session["Cart"];
foreach (DataRow objDR in objDT.Rows) { if (objDR["product"] == product) { // objDR["Quantity"] = int.Parse(objDR["Quantity"].ToString()) + int.Parse(txtQuantity.Text); objDR["Quantity"] = int.Parse(objDR["Quantity"].ToString()) + 1; blnMatch = true; } } if (blnMatch == false) { objDR = objDT.NewRow(); objDR["Quantity"] = quantity; objDR["Product"] = productName; objDR["Cost"] = Decimal.Parse(productcost); objDT.Rows.Add(objDR); } Session["Cart"] = objDT; mydg.DataSource = objDT; mydg.DataBind(); lbl.Text = "????" + GetItemTotal(); }
|
at pink line i get the error
Object reference not set to an instance of an object.
|
how can i set that?
Sam HobbsPosted Nov 17, 2009, 11:46 AM
Bahar JalaliPosted Nov 18, 2009, 3:22 AM
all ideas were good and help me
i change the datagrid with a gridview and i forgot change it in the code
but by your help i understand that
thanks for advance
jingePosted Nov 17, 2009, 7:40 PM
If the datatable is null, then such exception can not be happened. It makes sense to assign the DataSource to null. The only possible cause is the datagrid is a null reference.
Kirtan PatelPosted Nov 17, 2009, 11:30 AM
this can be happening because of your Session["Cart"] may be returning null.
check if Session["Cart"] is Null ???
because it can be the cause of problem .
if (Session["Cart"] !=null)
{
//All your Code Here
}
else
{
Response.Write("Cart is Null");
}
if my answer helps you then "check Do you like this answer check box" please :)
if still having problem then let me know .
or
you can send me your project Files(Zip) with database at [email protected]
i will resolve your problem :)