简体   繁体   中英

C# List Elements in the Session Not Responding

I'm doing a simple program to add a student(with ID,Name) to a List, then to search Student by ID through session.

Add Student Module is like below,

protected void addStudent(object sender, EventArgs e)
        {
            List<Student> thisstdlist = new List<Student>();
            thisstdlist = (List<Student>)Session["stdlist"];
            thisstdlist.Add(new Student(txtsid.Text,txtsname.Text));
            Session["stdlist"] = thisstdlist;
            Response.Redirect("Home.aspx");


        }

Search Student Module is Like Below,

  protected void searchStudent(object sender, EventArgs e)
        {


            foreach (Student element in (List<Student>)Session["stdlist"])
            {
               if(element.getID().Equals(txtstdid.Text)){
                   txtstdname.Text = element.getName();
               }
           }
        }

Student Class is like below,

public class Student
    {
        private String Name;
        private String ID;

        public Student(String sid, String sn) {

            this.Name = sn;
            this.ID = sid;

        }

        public String getName() {

            return this.Name;
        }
        public String getID()
        {

            return this.ID;
        }


    }

But when I added students, for ex: 100,John and Search by 100 it gives me no result. Please can anyone show me the mistake or the correct way of doing this.

are you setting breakpoints and actually checking what the values of these lists and what is actually stored in the session?

.Equals() is not doing what you think it is

try :

 foreach (Student element in (List<Student>)Session["stdlist"])
            {
               if(element.ID == txtstdid.Text){
                   txtstdname.Text = element.getName();
               }
           }

The add Student module won't initialize the student list correctly - you are creating a new List<Student> and then throwing the new list away with the next line assignment. I would go with something like:

var thisstdlist = (List<Student>)Session["stdlist"];
// If a key isn't found in Session, it will be null ..
if (thisstdlist == null)
{
    // i.e. only re-initialize if it was missing from session
    thisstdlist = new List<Student>();
    // You don't need to continually reassign the session variable
    Session["stdlist"] = thisstdlist;
}
// Adds to the list; now that Session also has a reference to the same list
thisstdlist.Add(new Student(txtsid.Text,txtsname.Text));

As per the comment, note that c# has automatic (albeit mutable) properties - you don't need the Java-style getters and setters.

public class Student
{
    public Student(string sid, string sn) 
    {
        Name = sn;
        ID = sid;
    }

    public string Name 
    {
        get;
        set;
    }
    public string ID
    {
        get;
        set;
    }
}

Also, in .Net, == for strings is overridden to test values (unlike Java's reference equality for strings), so you can rewrite the comparison as:

if (element.ID == txtstdid.Text)
{
    txtstdname.Text = element.Name;
}

Re : foreach - I guess means that you are using the List in a Dictionary (HashMap) fashion - if you use Dictionary instead of List - this will allow you do remove the foreach in favour of:

// addStudent ...
var txtstdname = new Dictionary<string, Student>();
 // ... 
txtstdname.Add(txtsid.Text, new Student(txtsid.Text,txtsname.Text))

// searchStudent ...
Student element = null;
if (txtstdname.TryGetValue(out element))
{
    txtstdname.Text = element.Name();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM