Adding Object in Arraylist

Last post 05-28-2008 12:09 AM by tralsi@gmail.com. 6 replies.

Sort Posts:

  • Adding Object in Arraylist

    05-12-2008, 12:21 AM

    Hi !

    I am novice asp.net, C# developer. I am learning to build QUIZ app as seen from one of the videos on this site.

    I have a Quiz Start form where I am initializing my session variables. the code is as under.

    I have a public class Answer which has QuestionID, CorrectAnswer , UserAnswer, Result members.

    in Page_Load  of Quiz Start page i have written code as under.

    ArrayList al = new ArrayList();

    Session.Add("AnswerList", al);

    Session.Add("QuizID", 1);

    upon pressing StartQuiz Button on this QuizStartPage Questions.aspx page loads.

    in Questions.aspx page I have a Question , 4 answers  & a Next button.

    on Click event of next button I have written

    System.Data.DataRowView drv = (System.Data.DataRowView ) QuestionDetails.DataItem;

    Answer ans = new Answer();

    ans.QuestionID = drv["QueID"].ToString();

    ans.CorrectAnswer = drv["CorrectAns"].ToString();

    ans.UserAnswer = UserAns.SelectedValue.ToString();

    Arraylist al = (ArrayList) Session["AnswerList"];

    al.Add(ans);

    Session.Add("Answerlist", ans);

    .

    .

    .When I run this & click on trhe next button I can see my question & 4 options. When I click on next it displays second question& its 4 probabale ansers but now when I click on next button, I get "Unable to cast object of type 'Answer' to type 'System.Collections.ArrayList'. Error.

    Kindly let me know where I am wrong. What do i need to rectify.

     

    Tralsi 

     

     

     

  • Re: Adding Object in Arraylist

    05-12-2008, 1:22 AM

    tralsi@gmail.com:

    Arraylist al = (ArrayList) Session["AnswerList"];

    al.Add(ans);

    Session.Add("Answerlist", ans);

    Of ans is not an array list you already added list inside Session, you don;t need to add the class into Sessions.

     

    Cheers
    Al
    My Blog
    GeoTwitter.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: Adding Object in Arraylist

    05-12-2008, 11:37 PM

    Thanks a lot for yur response albert !

    unfortunately I could not understand your explanation.

    What I want to do is, I want to store no of objects (ans) of type Answer into session. Below mentioned is my code.

    It says "can not convert Answer type to arraylist".  kindly rectify the line from belowmentioned code so that I can proceed.

    System.Data.DataRowView drv = (System.Data.DataRowView ) QuestionDetails.DataItem;
    Answer ans = new Answer();  //Answer is a public class  
    ans.QuestionID = drv["QueID"].ToString();
    ans.CorrectAnswer = drv["CorrectAns"].ToString();
    ans.UserAnswer = UserAns.SelectedValue.ToString();
    Arraylist al = (ArrayList) Session["AnswerList"];
    al.Add(ans);
    Session.Add("Answerlist", ans);
    
    

     

     

  • Re: Adding Object in Arraylist

    05-13-2008, 9:28 AM
    • Loading...
    • Puk Gooble
    • Joined on 04-16-2008, 11:06 AM
    • Posts 9

     This line

    Session.Add("Answerlist", ans);

    is putting an object of type Answer into the session, replacing it with the array list.  

    Did you mean:

    Session.Add("Answerlist", al);

  • Re: Adding Object in Arraylist

    05-14-2008, 11:44 AM

    Basically I want to store no of objects (ans) into an arraylist al.

    so that my al(0) would be lets say ans1, al(1) would be ans2 and so on.

    so what would be the way to store object it in an arraylist

     

  • Re: Adding Object in Arraylist

    05-15-2008, 1:54 AM
    Answer
    • Loading...
    • Dotnetfreaks
    • Joined on 05-15-2008, 4:56 AM
    • Delhi, India
    • Posts 16
    Hi mate,
    following are the issues that i see in your code:
    1. The Answer class should be Serializable add Serializable attribute to class definition.
    [Serializable]
    public class Answer 
    {
     //class body


    2. ArrayList is mis-spelled as "Arraylist" [L here should be in capital casing]
    3. Type casting should be like this - Session["AnswerList"] as ArrayList;
    4. You need to store answer collection in session not answer class object; 
    System.Data.DataRowView drv = (System.Data.DataRowView ) QuestionDetails.DataItem;
    Answer ans = new Answer(); //Answer is a public class
    ans.QuestionID = drv["QueID"].ToString();
    ans.CorrectAnswer = drv["CorrectAns"].ToString();
    ans.UserAnswer = UserAns.SelectedValue.ToString();
    ArrayList al = Session["AnswerList"] as ArrayList;
    al.Add(ans);
    Session["Answerlist"]= al;
    Ajander Singh
    http://blogs.dotnetfreaks.net 
    ---------------------------------------------------------------
    http://blogs.dotnetfreaks.net

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Adding Object in Arraylist

    05-28-2008, 12:09 AM

    Dear Ajander !

    Thanks very much for your reply.

    I have tried the above mentioned code.

    ArrayList al = Session["AnswerList"] as ArrayList;

    al.Add(ans);

     

    but still while running the application i am getting the error

    Saying NullReferenceException was unhandled by user code

    Object reference not set to an instance of an object.

    Trouble shooting tips :

    Use the new keyword to create an object instance

    I am using visual web developer 2008.

    as you know basicallyI want to add each time an answer object in a session. for that I am storing the values in ans object's variables. then want to store this ans object in an arrayList and add this arrayList in the session

    what is missing. or how do I handle this error

Page 1 of 1 (7 items)