1

I am trying to retrieve all Names, from the Names tables in the database. I am unable to retrieve the data and return it as a lIst. how can i do it ?

public List<SelectListItem> getNames()
        {
             try
            {
                using (SqlCommand com = new SqlCommand("SELECT * FROM Names", con))
                {
                    con();
                    SqlDataReader dr = com.ExecuteReader();

                    return ?? // How to return the items that was returned 

                }
            }
 .......
user1315906
  • 3,374
  • 8
  • 30
  • 43
  • possible duplicate http://stackoverflow.com/questions/1464883/how-can-i-easily-convert-datareader-to-listt – bas Feb 18 '13 at 19:45

4 Answers4

1

You can iterate over all rows returned as follows:

var items = new List<SelectListItem>();
while (dr.Read()) 
{
  var valueInColumn1 = dr[1];
  var valueInNamedColumn = dr["ColumnName"];
  ...
  items.Add(new SelectListItem { Text = valueInColumn1.ToString(), Value = valueInNamedColumn.ToString()); 
}
return items;
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
1

First instantiate the list to hold your items (you could also leave it null but that depends on what your callers expect) and then iterate over the datareader by calling Read() until it returns false, which means no more records are available.

When the datareader has records you can fetch a column by calling one of the methods GetString, GetInt, GetLong etc supplying it the column you want to fetch as a parameter.

Construct the type you want to store in your list and add the retrieved values to its properties, add the new type to the List.

public List<SelectListItem> getNames()
{
    var list = new List<SelectListItem>();
    try
    {
       using (SqlCommand com = new SqlCommand("SELECT * FROM Names", con))
       {
           con();
           SqlDataReader dr = com.ExecuteReader();
           while (dr.Read())
           {
               list.Add(new SelectListItem { 
                          Value = dr.GetString(0), // first column, depends on your table 
                          Text = dr.GetString(1)  // second column, depends on your table
                }); 
           }
    catch(Exception e)
    {
        Trace.WriteLine(r.Message);
    }
    return list;
}
rene
  • 41,474
  • 78
  • 114
  • 152
0

See my code example:

  public static List<ActionItem> GetAllActions()
        {
            var actionItems = new List<ActionItem>();
            SqlDataReader actionsReader = CatalogDB.GetAllActions();

            try
            {
                while (actionsReader.Read())
                {
                    actionItems.Add(new ActionItem
                                        {
                                            Id = (int)actionsReader["Id"],
                                            Name = actionsReader["Name"] != DBNull.Value ? (string)actionsReader["Name"] : null,
                                            Description = (string)actionsReader["Description"],
                                            CreationDate = (DateTime)actionsReader["CreationDate"]                                                
                                        }
                        );
                }
            }
            finally
            {
                actionsReader.Close();
            }

            return actionItems;
        }
algreat
  • 8,592
  • 5
  • 41
  • 54
  • Doesn't matter. It can be any type. It's just an example. Or you aren't able to write your code following the example? – algreat Feb 18 '13 at 20:26
0

There are a couple of different ways, but this is probably the most straight forward.

public List<SelectListItem> getNames()
{
    var list = new List<SelectedListItem>();
    try
    {
        using (SqlCommand com = new SqlCommand("SELECT * FROM Names", con))
        {
            con();
            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                var item = new SelectedListItem();
                item.Value = dr[0];
                list.Add(item);
            }
        }
    }
    catch(Exception ex)
    {
        // ...
    }

    return list;
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331