c# - new item adding to List overriding the previous one -


this question has answer here:

i using list<> in c# . want add values in list . problem first item add when second item insert same value ovverride first 1 , example first item abc inserted succefully when second item xyz came ovveride abc xyz , both items shows xyz.here code.

datatable dtbl3 = new datatable();    list<cartitems> lst = (list<cartitems>)session["mycart"]; dtbl3 = dal.get("select * mytable"); list<emailclass>  lstcstmer = new list<emailclass>(); (int j = 0; j < lst.count; j++) {               emaillst.__emailcstname = dtbl3.rows[0]["cstm_name"].tostring();     emaillst.__emailcstlname = dtbl3.rows[0]["cstm_lname"].tostring();     emaillst.__emailcstaddress = dtbl3.rows[0]["cstm_addr"].tostring();     emaillst.__emailcstphoneno = dtbl3.rows[0]["cstm_phone"].tostring();     emaillst.__emailcstcellno = dtbl3.rows[0]["cstm_cellno"].tostring();     emaillst.__emailcstskypid = dtbl3.rows[0]["cstm_skypeid"].tostring();     emaillst.__emailcstemail = dtbl3.rows[0]["cstm_email"].tostring();     emaillst.__emailcstcountry = dtbl3.rows[0]["cstm_country"].tostring();     emaillst.__emailcstcity = dtbl3.rows[0]["cstm_city"].tostring();     emaillst.__emailcstzipcode =convert.toint32( dtbl3.rows[0]["cstm_zipcode"].tostring());     emaillst.__emailcstremarks = dtbl3.rows[0]["cstm_remarks"].tostring();            emaillst._emailcartprodname = lst[j]._cartprodname;     emaillst._emailcartprodprice = lst[j]._cartprodprice;     emaillst._emailcartprodqnty = lst[j]._cartprodqnty;     emaillst._emailcartprodcode = lst[j]._cartprodname;     emaillst._emailtotalprodprice = lst[j]._totalprodprice;     lstcstmer.add(emaillst);            } 

you're adding same item on , on again, , because it's reference type entries in list point same instance of emailclass.

create new instance in every loop iteration fix that:

for (int j = 0; j < lst.count; j++) {     emaillst = new emailclass();      emaillst.__emailcstname = dtbl3.rows[0]["cstm_name"].tostring();     emaillst.__emailcstlname = dtbl3.rows[0]["cstm_lname"].tostring();     emaillst.__emailcstaddress = dtbl3.rows[0]["cstm_addr"].tostring();     emaillst.__emailcstphoneno = dtbl3.rows[0]["cstm_phone"].tostring();     emaillst.__emailcstcellno = dtbl3.rows[0]["cstm_cellno"].tostring();     emaillst.__emailcstskypid = dtbl3.rows[0]["cstm_skypeid"].tostring();     emaillst.__emailcstemail = dtbl3.rows[0]["cstm_email"].tostring();     emaillst.__emailcstcountry = dtbl3.rows[0]["cstm_country"].tostring();     emaillst.__emailcstcity = dtbl3.rows[0]["cstm_city"].tostring();     emaillst.__emailcstzipcode =convert.toint32( dtbl3.rows[0]["cstm_zipcode"].tostring());     emaillst.__emailcstremarks = dtbl3.rows[0]["cstm_remarks"].tostring();      emaillst._emailcartprodname = lst[j]._cartprodname;     emaillst._emailcartprodprice = lst[j]._cartprodprice;     emaillst._emailcartprodqnty = lst[j]._cartprodqnty;     emaillst._emailcartprodcode = lst[j]._cartprodname;     emaillst._emailtotalprodprice = lst[j]._totalprodprice;      lstcstmer.add(emaillst);  } 

Comments