Maha Seleem
Maha Seleem

Reputation: 35

How can I select items from my table in to a List<T>?

How can I get the results from my data reader into a List<String>?

Here is my code so far:

public List<string> Item_Getall()
{
    List<string> data = new List<string>();
    SqlCommand cmd = new SqlCommand("c_get_all_item",oo.conn);
    cmd.CommandType = CommandType.StoredProcedure;
    oo.conn.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        data.Add(rdr["item_name_id_pk"].ToString());
        data.Add(rdr["item_name_arabic"].ToString());
        data.Add(rdr["item_componant"].ToString());
        data.Add(rdr["item_componant_arabic"].ToString());
        data.Add(rdr["item_price"].ToString());
        data.Add(rdr["item_image"].ToString());
        data.Add(rdr["item_category_name_id_fk"].ToString());
    }
    oo.conn.Close();
    return data;
}

Upvotes: 0

Views: 434

Answers (3)

Kaf
Kaf

Reputation: 33809

Using a DataAdapter and a custom type:

SqlCommand cmd = new SqlCommand("c_get_all_item",oo.conn);
cmd.CommandType = CommandType.StoredProcedure;
oo.conn.Open();

var adapter = new SqlDataAdapter(cmd);

DataTable dt;
adapter.Fill(dt);

oo.Close()

//Convert DataTable to a list of YourType
List<YourType> data = dt.AsEnumerable().Select(s=> 
                         new YourType { 
                          item_name_id_pk = s.Field<FieldDataType>("item_name_id_pk"),
                          item_name_arabic = s.Field<FieldDataType>("item_name_arabic"),
                             ...
                         })
                         .ToList();

Your custom type would be like;

public class YourType
{
    //Replace the DataType with correct datatype to match with FieldDataType
    public DataType item_name_id_pk {get; set;}
    public DataType item_name_arabic {get; set;}
    ...
}

Upvotes: 0

Laoujin
Laoujin

Reputation: 10229

I think you will want to create a custom class and return a list of this class:

public class Item
{
     public int Id { get; set; }
     public string Name { get; set; }
}

Reading would go as:

var data = new List<Item>();
while (rdr.Read())
{
    data.Add(new Item()
    {
        Id = int.Parse(rdr["item_name_id_pk"]),
        Name = rdr["item_name_arabic"].ToString()
    }
}
return data;

Also, look into the using() statement which will make your code more robust in the eye of exceptions during database calls.

Upvotes: 1

Adil
Adil

Reputation: 148110

You better use your custom type list instead of string and store your custom type object in list

List<YourCustomType> data = new List<YourCustomType>();

Custom type

public class YourCustom
{
     public string ItemName {get; set;}
     //Similarly all the properties.
}

Reading values from data Reader and adding in Custom Type List

while (rdr.Read())
{
    data.Add(new YourCustom()
    {
       Id = rdr["item_name_id_pk"].ToString(),
       Name = rdr["item_name_arabic"].ToString()
       //Similarly all other properties could be set 
    }
}

Upvotes: 2

Related Questions