c# - How to get values from a list<class> -
i have these classes, want use login, check if email , password same, redirect respective page.
public class account { public account(){} public int accid { get; set; } public string emailaddress { get; set; } public string password { get; set; } public string name { get; set; } public string company { get; set; } public string position { get; set; } public string department { get; set; } public string mobileno { get; set; } public string officeno { get; set; } } public static sadatareader doselectquery(string sql) { saconnection myconnection = new saconnection(db_str); //open connection myconnection.open(); //create command object. sacommand mycommand = myconnection.createcommand(); //specify query. mycommand.commandtext = sql; //create datareader command sadatareader reader = mycommand.executereader(); return reader; } public static list<account> getallaccountfromreader(sadatareader reader){ list<account> results = new list<account>(); while (reader.read()) { int accid = reader.getint32(0); string emailaddress = reader.getstring(1); string password = reader.getstring(2); string name = reader.getstring(3); string company = reader.getstring(4); string position = reader.getstring(5); string department = reader.getstring(6); string mobileno = reader.getstring(7); string officeno = reader.getstring(8); account accounts = new account(); accounts.accid = accid; accounts.emailaddress = emailaddress; accounts.password = password; accounts.name = name; accounts.company = company; accounts.position = position; accounts.department = department; accounts.mobileno = mobileno; accounts.officeno = officeno; results.add(accounts); } return results; } public static list<account> getallaccounts() { //specify query. string sql = "select accountid,emailaddress,password,name,company,position,department,mobileno,officeno account"; sadatareader reader = doselectquery(sql); list<account> results = getallaccountfromreader(reader); return results; }
.cs file check fields
protected void btnsubmit_click(object sender, eventargs e) { string email = tbemail.text; string password = tbpw.text; list<account> getaccounts = minutedb.getallaccounts(); // session["getallaccount"] = getaccounts; if(email ==?? && password == ??) { //session["name"] = name.tostring(); //session["id"] = convert.toint32(accid.tostring()); response.redirect("homepage.aspx"); } else if (email == "" && password == "") { scriptmanager.registerstartupscript(this, gettype(), "error", "alert('please enter login , password!');", true); } else { scriptmanager.registerstartupscript(this, gettype(), "error", "alert('wrong login or password!');", true); } }
how retrieve email , password list getaccounts can check if (email == email list account && password == password list account) ??
try linq/extension methods.
var account = minutedb.getallaccounts() .where(p=> p.emailaddress==email && p.password==password) .firstordefault(); if(account!=null) { session["id"]=account.accid; session["name"]=account.name; response.redirect("~/other_page.aspx"); }
write following code in other_page.aspx
read session key-value.
int id=0; string name=""; if(session["id"]!=null) id=int.parse(session["id"].tostring()); if(session["name"]!=null) name=session["name"];
ps: not store password in list<t>
. may assign account
object reference session.
e.g
if(account!=null) { session["account"]=account; response.redirect("~/other_page.aspx"); }
and retrieve account
value session:
account account=session["account"] account; if(account!=null) { //read property value of account }
Comments
Post a Comment