0

I have this function below, what I am trying to do, is connect to the database, run a query and put the results in a List and then return the list. This is what I came up so far,

 public class AirportClass
    {
        private string connectionString;
        private SqlConnection connection;
        private SqlCommand command;
        private Dictionary<string, List<string>> items;

        public AirportClass()
        {
            connectionString = @"Server=server;database=database;uid=username;pwd=password;";
        }

        public Dictionary<string, List<string>> getListItems()
        {
            items = new Dictionary<string, List<string>>();
            connection = new SqlConnection(connectionString);
            command = new SqlCommand("SELECT * FROM Table");
            command.Connection = connection;
            connection.Open();
            SqlDataReader dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                string data = dataReader[0].ToString();
                items.Add(data);
            }
            connection.Close();
            return items;
        }
    }

I have ran the debugger and I can see that I am getting a connection and data is getting returned, I am just have trouble putting the items into a list. I get error when I add the items.Add(data) saying No overload for method 'Add' takes 1 arguments....can anyone help me out please?

user3723240
  • 395
  • 3
  • 11
  • 29

3 Answers3

2

that's because items isn't a List<T>, it's a Dictionary If you want to add to a Dictionary, you need to provide a key (first parameter) and a value (second parameter)

since your value type of the dictionary is List<String>, the second parameter must be a List<string>

Something along the lines of

items.Add(dataReader[0].ToString(), dataReader.ToList().convertAll(itm => itm.toString()));
Timothy Groote
  • 8,614
  • 26
  • 52
  • note that you can't really cast a `DataReader` to `List` like that. if you want to know how to do that, look here : http://stackoverflow.com/questions/1464883/how-can-i-easily-convert-datareader-to-listt – Timothy Groote Aug 07 '14 at 13:28
  • I tried this `string data = dataReader[0].ToString(); items.Add(data, data.ToList().convertAll(itm => itm.toString()));` and get an error around convertAll, `System.Collections.Generic.List does not contain a definition for 'convertAll' – user3723240 Aug 07 '14 at 13:36
  • Read the comment i posted above yours... in a perfect world, we'd be able to solve that issue so simply, but unfortunately, it's not a perfect world. – Timothy Groote Aug 07 '14 at 14:27
2

Use a List and not a Dictionary:

 public class AirportClass
    {
        private string connectionString;
        private SqlConnection connection;
        private SqlCommand command;
        private List<string> items;

        public AirportClass()
        {
            connectionString = @"Server=server;database=database;uid=username;pwd=password;";
        }

        public List<string> getListItems()
        {
            items = new List<string>();
            connection = new SqlConnection(connectionString);
            command = new SqlCommand("SELECT * FROM Table");
            command.Connection = connection;
            connection.Open();
            SqlDataReader dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                string data = dataReader.GetValue(0).ToString();
                items.Add(data);
            }
            connection.Close();
            return items;
        }
    }
Bart van der Drift
  • 1,287
  • 12
  • 30
1

You need to supply a key and a value to a Dictionary.

Alan B
  • 4,086
  • 24
  • 33