4
Reply

Object reference not set to an instance of an object in gridview data source

Bahar Jalali

Bahar Jalali

Nov 17 2009 8:53 AM
10.6k
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?

Answers (4)