1

I am building a online shop for a school project and I came across this problem when I reached the adding product to cart of an item part. It doesn't shows any error is visual studio but when i run in the web browser it doesn't it work and it shows this error, I am trying to get the ProductID of my items in my productlist and I think there is something wrong in that part, Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.****strong text

Im new to ASP.Net so please help me

This my Code behind code:

string ProductID = Convert.ToInt32((((Button)sender).CommandArgument)).ToString();
string ProductQuantity = "1";

DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblavailablestock = currentItem.FindControl("lblavailablestock") as Label;
if (Session["MyCart"] != null)
{
    DataTable dt = (DataTable)Session["MyCart"];
    var checkproduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID")== ProductID);
    if(checkproduct.Count() == 0)

    {
        string query = "select * from Products where ProductID = " + ProductID + "";
        DataTable dtproducts = GetData(query);


        DataRow dr = dt.NewRow();
        dr["ProductID"] = ProductID;
        dr["Name"] = Convert.ToString(dtproducts.Rows[0]["Name"]);
        dr["Description"] = Convert.ToString(dtproducts.Rows[0]["Description"]);
        dr["Price"] = Convert.ToString(dtproducts.Rows[0]["Price"]);
        dr["ImageUrl"] = Convert.ToString(dtproducts.Rows[0]["ImageUrl"]);
        dr["ProductQuantity"] = ProductQuantity;
        dr["AvailableStock"] = lblavailablestock.Text;
        dt.Rows.Add(dr);

        Session["MyCart"] = dt;
        lblitems.Text = dt.Rows.Count.ToString();

    }
}
    else
    {
        string query = "select * from Products where ProductID = " + ProductID + "";
        DataTable dtproducts = GetData(query);

        DataTable dt = new DataTable();

        dt.Columns.Add("ProductID", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Description", typeof(string));
        dt.Columns.Add("Price", typeof(string));
        dt.Columns.Add("ImageUrl", typeof(string));
        dt.Columns.Add("ProductQuantity", typeof(string));
        dt.Columns.Add("AvailableQuantity", typeof(string));

        DataRow dr = dt.NewRow();
        dr["ProductID"] = ProductID;
        dr["Name"] = Convert.ToString(dtproducts.Rows[0]["Name"]);
        dr["Description"] = Convert.ToString(dtproducts.Rows[0]["Description"]);
        dr["Price"] = Convert.ToString(dtproducts.Rows[0]["Price"]);
        dr["ImageUrl"] = Convert.ToString(dtproducts.Rows[0]["ImageUrl"]);
        dr["ProductQuantity"] = ProductQuantity;
        dr["AvailableStock"] = lblavailablestock.Text;
        dt.Rows.Add(dr);

        Session["MyCart"] = dt;       
    }

My source code:

<asp:Button ID="btnaddtocart" runat="server" Text="Add to cart" OnClick="btnaddtocart_Click" CommandArgument='<%# Bind("ProductID") %>'/>
Jongware
  • 22,200
  • 8
  • 54
  • 100
Anonymous
  • 13
  • 3
  • what is the full error you get? – Damith Sep 24 '15 at 04:42
  • Apart from the error you mentioned you should also include the line at which it is encountered upon using a debugger – Rahul Jha Sep 24 '15 at 04:43
  • this is the full error i get when i checked the "Inspect Element" in the web browser – Anonymous Sep 24 '15 at 04:48
  • Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object. – Anonymous Sep 24 '15 at 04:48
  • Please help.. I got less than 2 weeks to finish this project – Anonymous Sep 24 '15 at 04:50
  • @Anonymous Are you getting an error while you are clicking on button or you are getting error on page load ? Can you debug the code and give us a hint that at which line number the error is occurring? Sample data and inner exception details will be helpful. – John Sep 24 '15 at 04:56
  • I am not sure, but do changes in this query, string query = "select * from Products where ProductID = ' " + ProductID + " ' "; , if u wont use ' ' then ur ProductID value acts as column name. – Anoop B.K Sep 24 '15 at 05:00
  • If u make use of [`Resharper`](https://www.jetbrains.com/resharper/) then u might find the number of possible object reference exception in above code snippet. – Ankush Madankar Sep 24 '15 at 05:18
  • @ManthanYes i get that error evry time i cliked the button – Anonymous Sep 24 '15 at 08:07

0 Answers0