0

I am doing a program where i have used a class with 4-5 attributes

public class Car
{
    public string Make;
    public string Model;
    public int Year;
    public int Doors;
    public string Colour;
    public float Price;
}

now from sql query I want o fill the value of List and return same like this..

List<Car> lrs = null;


while (rdr.Read())
                        {
                            lrs = new List<LocationResult>{ new LocationResult{Make=(string)rdr["Make"], Model=null, Year=null, Doors=null, Colour=null, Price=null}};
                        }

Everything is working fine with code except, that lrs only contains the last row of sql after above run completes. It get overwritten with while loop each time. So how am i supposed to keep adding new rows ..

if i do..

lrs.add("") - it only accepts one parameter ....

Any help with be appreciated...

Luckyy
  • 1,021
  • 4
  • 15
  • 29

1 Answers1

0
List<Car> lrs = new List<Car>();

while (rdr.Read()) {

  Car c = new Car();
  c.Model = rdr. ... /* get model from reader...*/;
  c.Make = rdr. ... /* get make from reader...*/;
  /* ... */

  lrs.add(c);
}
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • That did the job,. thanks Hanno..just a curiosity isn't there one liner code which we can do rather adding values one by one..for 6-7 properties ...? – Luckyy Dec 24 '12 at 14:59
  • Of course you may declare a constructor for `Car` which accepts and sets all properties in one call, like `Car( model, make, year, ... )`. – JimmyB Dec 24 '12 at 15:01