c# - Compare a list of objects to an object -


i have button add cv. when user logs on website, check table "cv" {id_member, id_cv}, if id_member exists in table, add cv button disabled, otherwise user can click it.

i retrieve id_members db in list (c) of classes (cv { int id_candidat}). need check existence of logged user's id in list (extracted session variable).

this how it, it's not working:

protected void page_load(object sender, eventargs e) {     list<cv> c = new list<cv>();      sqlconnection con = new sqlconnection(@"data source=p5-pc\sqlexpress;" +                "initial catalog=recrutement_online_3;integrated security=true");     sqlcommand cmd = new sqlcommand();     cmd.connection = con;     con.open();     cmd.commandtext = "select id_candidat cv";     sqldatareader dr = cmd.executereader();      while (dr.read())     {         cv p3 = new cv();         p3.id_candidat = int.parse(dr[0].tostring());         c.add(p3);             }     dr.close();     con.close();      cv r = new cv();     r.id_candidat = int.parse(session["id_candidat"].tostring());     if (c.contains(r))     {         button1.enabled = false;     }      ... 

my question is, how can check existance of logged user's cv in database?

you can check candidate existence when filling list of available cvs:

... bool candidatexists = false; int idcandidat = int.parse(session["id_candidat"].tostring()); while (dr.read())     {         cv p3 = new cv();         p3.id_candidat = int.parse(dr[0].tostring());         c.add(p3);         if(p3.id_candidat == idcandidat)         {              candidatexists = true;         }     }     dr.close();     con.close();      button1.enabled = !candidatexists; 

Comments