I am deleting item from cart through Jquery ajax,but it is not removing. It is returning "System.String[]".
  [WebMethod(EnableSession = true)]
  public string ClearItem(string Img)
    {
        try
        {
            if (Session["Items"] != null)
            {
                string[] session_item_arr = Session["Items"].ToString().Split('|');
                for (int i = 0; i < session_item_arr.Length; i++)
                {
                    string[] key_arr = session_item_arr[i].Split(',');
                    foreach (string item in key_arr)
                    {
                        if (item == Img)
                        {           
                            session_item_arr = session_item_arr.Where(val => val != session_item_arr[i]).ToArray();
                        }
                    }
                    bool item_inedx = Array.Exists(session_item_arr, x => x == session_item_arr[i]);
                    if (item_inedx)
                    {
                        Session["Items"] = session_item_arr[i];
                    }
                    else
                    {
                        Session["Items"] = session_item_arr;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Session["Items"].ToString();
    }

Recommended Answers

What is the value of Session["Items"]?

Jump to Post

All 3 Replies

What is the value of Session["Items"]?

It seems your array is empty, you are doing a check to see if it is not empty, try and check if it is empty i.e. alert("Oops, array is empty");

Then you know to go back to where you have created teh array, there might be a problem there.

From your code:
Session["Items"].ToString().Split('|');

I assumed that Session["Items"] is a string where its content is get through concat the session_item_arr array with '|'

Therefore the return should be String.Join("|", session_item_arr)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.