Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# list
i have a list
 
List<SalesDetail>  SalesList = new List<SalesDetail>();
SalesDetail detail = new SalesDetail();  
 
where "SalesDetail" is a class. i have a button (add) and my code on click event of add button is SalesList.Add(details); where details is object of class SalesDetail which contains public variables with {set; and get;}
 
but when i try to retrieve each item of the list then i only get the last item. my code retrieving each item is
 
foreach(SalesDetail sd in SalesList)
{
 
    messageBox.show(sd);
 
}
 
in my class SalesDetail i have following code
Public string Brand{get; set;}
Public string Product{get; set;}
 
i want to retrieve each item from list and save it to database i would like to know where i have made mistake while retrieving the data..
please help
Regards bunzitop
Posted 26-Mar-13 4:59am
Edited 26-Mar-13 5:20am
v2
Comments
Prasad Khandekar at 26-Mar-13 10:13am
   
You might want to change messageBox.show(SalesList) to messageBox.show(sd)
Orcun Iyigun at 26-Mar-13 10:35am
   
You might want to change SalesList.Add(details); to SalesList.Add(detail); as well. How many items do have in your list? Have you checked it with a debugger. because from your explanation and code it seems ok.
bunzitop at 26-Mar-13 10:49am
   
yes i have checked with debugger and the count is two and its right beacause i have inserted two items but when retrieving i only get last inserted item
OriginalGriff at 26-Mar-13 12:46pm
   
Show your code for adding the items, and how it relates to the code for showing the list content.
There is no obvious error in the code fragments you show, so it must be else where.
Where did you check with the debugger?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try Below code:-
 
List SalesList = new List();
 
SalesDetail detail = new SalesDetail();
detail.Brand = "123";
detail.Product = "12345";
 
SalesList.Add(detail);
 
SalesDetail detail1 = new SalesDetail();
detail.Brand = "233";
detail.Product = "56345";
 
SalesList.Add(detail1);
 
Now to Fetch Data :_--
 
foreach(SalesDetail sd in SalesList)
{
 
messageBox.show(sd.Brand + sd.Product );
 
}
 
It will show two records one by one.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100