Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,
I am getting exception on the bold Line in the code below.
Item_ID column in database is nvarchar(50). I haven't used dictionary before, this code is from someone else and i am trying to use it.

private static void FIllGroupID(string connectionString)
            string queryStringNoGroupID =
                "Use Items select * from table_items_shelves where Item_ID is Null or Item_ID = '';";

            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand CommandGetAllWithoutID = new SqlCommand(queryStringNoGroupID, connection);

            DataTable DataTableAllWithoutID = new DataTable();

            SqlDataAdapter adapterAllWithoutID = new SqlDataAdapter(CommandGetAllWithoutID);


            //this code will get empty items
            List<DataRow> nullOrEmpty = DataTableAllWithoutID.AsEnumerable()
                .Where(x => x.Field<object>("Item_ID") == null)
            //this creates a dictionary of valid items
            Dictionary<object, List<DataRow>> dict = DataTableAllWithoutID.AsEnumerable()
                .Where(x => x.Field<object>("Item_ID") != null)
                .GroupBy(x => x.Field<object>("Item_ID"), x => x)
                .ToDictionary(x => x.Key, x => (List<DataRow>)x.ToList());
            //create IEnumerator for the null items
            IEnumerator<DataRow> emptyRows = nullOrEmpty.GetEnumerator();
            Boolean noMoreEmptyRows = false;
            if (emptyRows != null)
                foreach (object key in dict.Keys)
                    //get count of items
                    int count = dict[key].Count;

//exception on this line

                    int itemID = (int)key;

                    for (int index = count; count < 8; count++)
                        //get an item from the null list
                        emptyRows.Current["Item_ID"] = itemID;
                        Console.WriteLine("current item ID "+itemID);
                        if (emptyRows.Current == null)
                            noMoreEmptyRows = true;
                    if (noMoreEmptyRows == true)
            //now test if there are still items that contains empty items
            if (emptyRows.Current != null)
                //add code to create new group

Well, that's probably because it isn't a int - exactly what it is, I'm not sure - and we'd need your SQL data to have a clue of working it out.

Instead, put a breakpoint on the line, and examine exactly what type of object key is. When you know that, you will be in a better position to work out what kind of cast you need.
Sorry - but that is about the best answer we can give, from that information!
Share this answer
As per the code your Dictionary is of type Dictionary<object, List<DataRow>> and you are iterating through the Keys , with in the for each you need to get the List<DataRow>

var dataRows = dict[key]

You will have to iterate over the datarows

foreach(var row as DataRow in dataRows){

int itemID = (int)row["Item_ID"];

Share this answer
Check this:
Share this answer
Covert.ToInt32(key) insted of (int)key solved the problem.
Share this answer
khurram ali lashari 9-Aug-14 8:34am    
Thanks @Mubshir its worked for me . would you Please explain me why (int) this type casting is not working ... ???????

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

  Print Answers RSS

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