Tuesday, 15 May 2012

c# - List unable to hold the data of database -



c# - List<> unable to hold the data of database -

i have sql table named "employee". table has 4 fields: emplid,empname,empcontact , empemail. creating asp.net mvc application. in model, have users class given below:-

class users { public string id{get; set;} public string name{get;set;} public string contact{get;set;} public string email(get;set;} }

now code take info database using ado.net , send controller given below:-

class databaseaccess { public list<users> getdata() { list<users> eachemployee = new list<users>; users user = new users(); sqlconnection conn = new sqlconnection(connectionstring); con.open(); sqldataadapter adp = new sql info adapter("select * employee",conn); dataset ds = new dataset(); adp.fill(ds,"employee"); for(int i=0;i<ds.tables[0].rows.count;i++) { user.id = ds.tables[0].rows[i]["empid"].tostring(); user.name = ds.tables[0].rows[i]["empname"].tostring(); user.contact = ds.tables[0].rows[i]["empcontact"].tostring(); user.email = ds.tables[0].rows[i]["empemail"].tostring(); eachemployee.add(user); } homecoming eachemployee; }

now problem eachemployee contains rows same data. if table has 2 rows given below:

emp1, gaurav, 888888888,abc@gmail.com emp2, sharma, 999999999,xyz@gmail.com

then eachemployee list contains 2 rows same data. both rows consists of emp2 info given below:

emp2, sharma, 999999999,xyz@gmail.com emp2, sharma, 999999999,xyz@gmail.com

how can out above issues? didn't know reason that?

move your

users user = new users();

inside loop.

in code, creating 1 object, , modifying , adding several times list...

c# asp.net-mvc ado.net

No comments:

Post a Comment