0

I am using List<> in C# . I want to add values in the List . But problem is that first item add successfully but when second item insert the same value ovverride into the first one , example first item abc inserted succefully but when second item xyz came it ovveride abc to xyz and both items shows xyz.Here is my code.

DataTable dtbl3 = new DataTable();   
List<CartItems> lst = (List<CartItems>)Session["mycart"];
dtbl3 = DAL.Get("Select * from mytable");
List<EmailClass>  lstCstmer = new List<EmailClass>();
for (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);           
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
farhan farhan
  • 39
  • 2
  • 7
  • Where did you initialize `emailLst` instance? – Shaharyar Jul 16 '15 at 18:26
  • http://stackoverflow.com/questions/13659284/c-sharp-list-overwrite-issue, http://stackoverflow.com/questions/2156482/why-does-adding-a-new-value-to-list-overwrite-previous-values-in-the-list, http://stackoverflow.com/questions/19156612/ilist-add-overwriting-existing-data, and so on. Search the web for "C# loop list add overwrite" and you'll find plenty of results. – CodeCaster Jul 16 '15 at 18:32
  • @Shaharyar emailLst is an instace of Class – farhan farhan Jul 16 '15 at 23:22

1 Answers1

2

You're adding the same item over and over again, and because it's a reference type all entries in the list point to the same instance of EmailClass.

Create a new instance in every loop iteration to 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);

}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263