locked
What is wrong with my remove button RRS feed

  • Question

  • hi I've just started learning ASP.NET C#  and I 'm working with excersice . There is something wrong with my add and remove button , but I don't know how to fix it

    now I'm working with my remove button

    in the remove button I'm trying to retrive ordered arraylist from session and then loop through the arraylist and find the object with the same id as what the user input in the textbox and then display the updated arrayList and save it in the session object

        1. protected void RemoveButton2_Click(object sender, EventArgs e)
        2. {
        3. ArrayList ar = new ArrayList();
        4. int id1 = int.Parse(TextBox2.Text);
        5. if (Session["items"] != null)
        6. {
        7. Label1.Text = (String)Session["items"];
        8. foreach (OrderedItem s in Session)
        9. {
        10. if (s.ID == id1)
        11. {
        12. ar.Remove(s);
        13. }
        14. else
        15. {
        16. Label2.Text = "Id not found";
        17. }
        18. Session["items"] = ar;
        19. DisplayList(ar);
        20. }
        21. }
    1. }

      • Edited by will-99 Saturday, February 9, 2013 3:45 PM
      • Moved by Mike Feng Wednesday, February 13, 2013 3:14 AM
      Saturday, February 9, 2013 1:02 PM

    Answers

    • Hi Will-99,

      You use "Session["items"]=savedItem;" in AddButtonClick method and the type of savedItem is ArrayList, and in removeButtonClick method, you try to convert Session["items"]  to string using "Label1.Text = (String)Session["items"];", It's the reason.


      mark kang


      • Edited by mark kang Saturday, February 9, 2013 4:04 PM
      • Proposed as answer by Christopher84 Saturday, February 9, 2013 5:55 PM
      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Saturday, February 9, 2013 3:57 PM
    • A session is always an object.

      The problem is that you can convert the array to a string.  You should always cast to the appropriate type when reading session values.

       

      Noam B.



      Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

      • Proposed as answer by Noam B Monday, February 11, 2013 2:28 PM
      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Saturday, February 9, 2013 6:55 PM
    • Hi will-99,

      Welcome to the MSDN forum.

      You may try the ASP.NET forums as well for questions about ASP.NET.

      ASP.NET Forums

      http://forums.asp.net/

      Best Regards,


      Yoyo Jiang[MSFT]
      MSDN Community Support | Feedback to us
      Develop and promote your apps in Windows Store
      Please remember to mark the replies as answers if they help and unmark them if they provide no help.

      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Tuesday, February 12, 2013 10:00 AM

    All replies

    • You've lost me in your remove function.   You create an new (empty) ArrayList  called ar and you never put anything into it but loop goes and attempts to remove  something from it, which is harmless, but probably not what you wanted.

      Did you miss a line where you set ar to Session["items"]?


      • Edited by Ron Natalie Saturday, February 9, 2013 2:10 PM
      Saturday, February 9, 2013 2:10 PM
    • can add and remove work inside loop or should I change it and how can I have the if statment outside the loop 

      Saturday, February 9, 2013 2:53 PM
    • I got this error

      Unable to cast object of type 'System.Collections.ArrayList' to type 'System.String'.

        

       

      Saturday, February 9, 2013 2:57 PM
    • Hi Will-99,

      You use "Session["items"]=savedItem;" in AddButtonClick method and the type of savedItem is ArrayList, and in removeButtonClick method, you try to convert Session["items"]  to string using "Label1.Text = (String)Session["items"];", It's the reason.


      mark kang


      • Edited by mark kang Saturday, February 9, 2013 4:04 PM
      • Proposed as answer by Christopher84 Saturday, February 9, 2013 5:55 PM
      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Saturday, February 9, 2013 3:57 PM
    • A session is always an object.

      The problem is that you can convert the array to a string.  You should always cast to the appropriate type when reading session values.

       

      Noam B.



      Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

      • Proposed as answer by Noam B Monday, February 11, 2013 2:28 PM
      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Saturday, February 9, 2013 6:55 PM
    • Hi will-99,

      Welcome to the MSDN forum.

      You may try the ASP.NET forums as well for questions about ASP.NET.

      ASP.NET Forums

      http://forums.asp.net/

      Best Regards,


      Yoyo Jiang[MSFT]
      MSDN Community Support | Feedback to us
      Develop and promote your apps in Windows Store
      Please remember to mark the replies as answers if they help and unmark them if they provide no help.

      • Marked as answer by Just Karl Wednesday, October 2, 2013 10:08 PM
      Tuesday, February 12, 2013 10:00 AM