Click here to Skip to main content
15,879,002 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 26-Mar-13 4:20am
v2
Comments
Prasad Khandekar 26-Mar-13 10:13am    
You might want to change messageBox.show(SalesList) to messageBox.show(sd)
Orcun Iyigun 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 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 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

Try Below code:-

List<salesdetail> SalesList = new List<salesdetail>();

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900