Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello House,

I want to be able to add two list items into dictionary, below is my code:

XML
var attributeValues = new Dictionary<string   , string >();
                    var listAttributeName = new List<string>();
                    var listAttributeRegion = new List<string>();
                    var listAttributeIdSplit = new List<string>();



                    var command = new SqlCommand("select [AttrName], [RegionName] from csowner.CatRegionMap where CatName = 'lands'",connection);
                    command.CommandType = CommandType.Text;
                    connection.Open();

                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        listAttributeName.Add(reader["AttrName"].ToString().ToUpper());
                        listAttributeRegion.Add(reader["RegionName"].ToString());


                        attributeValues.Add( listAttributeName.Add( reader["AttrName"] ), listAttributeRegion.Add( reader["RegionName"] ) );


                    }



i tried adding listAttributeName and listAttributeRegion to attributeValues, it is giving exception, please is there anyway i can do this?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jun-14 10:42am    
It looks like you lost part of your code due to wrong HTML formatting... make sure you escape all <>
But... the answer is: the same way as any other type.
—SA

1 solution

If you are interested in adding the list items to Dictionary then, you have to do something like below

attributeValues.Add( reader["AttrName"].ToString(), reader["RegionName"].ToString() );


Since your dictionary is declared with key and value as string, you will have to add string items into it.

Hope it helps you.
 
Share this answer
 
Comments
Uwakpeter 20-Jun-14 10:15am    
it is now working, please how can i retrieve attrName with corresponding RegionName from the dictionary?
Sergey Alexandrovich Kryukov 20-Jun-14 10:43am    
The question was about adding a list, but you are adding to strings as key/value pair... However, it could be a confusion in the question... Anyway, there is no such problem... :-)
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900