1

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.

Dilukshan Mahendra
  • 3,230
  • 7
  • 41
  • 62

2 Answers2

4

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();
               }
           }
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • You cannot call element.ID directly since it is private, so I used element.getID(), But Still No Result :( – Dilukshan Mahendra Feb 26 '14 at 04:40
  • sorry, I missed that. Then make it public , if you have a public function to get it anyways. OR... just call GetID() == – Scott Selby Feb 26 '14 at 05:18
  • +1. Java style `.Equals` [will still work](http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals) although obviously not a common convention in .Net, but a necessity in Java. – StuartLC Feb 26 '14 at 07:25
3

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();
}
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285