Click here to Skip to main content
13,092,862 members (97,411 online)
Rate this:
Please Sign up or sign in to vote.
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)

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 3:59am
Updated 26-Mar-13 4:20am
Prasad Khandekar 26-Mar-13 10:13am
You might want to change to
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

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

Solution 1

Try Below code:-

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

SalesDetail detail = new SalesDetail();
detail.Brand = "123";
detail.Product = "12345";


SalesDetail detail1 = new SalesDetail();
detail.Brand = "233";
detail.Product = "56345";


Now to Fetch Data :_--

foreach(SalesDetail sd in SalesList)
{ + sd.Product );


It will show two records one by one.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
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