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); connection.Open(); SqlCommand CommandGetAllWithoutID = new SqlCommand(queryStringNoGroupID, connection); DataTable DataTableAllWithoutID = new DataTable(); SqlDataAdapter adapterAllWithoutID = new SqlDataAdapter(CommandGetAllWithoutID); adapterAllWithoutID.Fill(DataTableAllWithoutID); //this code will get empty items List<DataRow> nullOrEmpty = DataTableAllWithoutID.AsEnumerable() .Where(x => x.Field<object>("Item_ID") == null) .ToList(); //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(); emptyRows.MoveNext(); Boolean noMoreEmptyRows = false; if (emptyRows != null) { foreach (object key in dict.Keys) { Console.WriteLine(key.ToString()); //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); Console.ReadKey(); emptyRows.MoveNext(); if (emptyRows.Current == null) { noMoreEmptyRows = true; break; } } if (noMoreEmptyRows == true) { break; } } } //now test if there are still items that contains empty items if (emptyRows.Current != null) { //add code to create new group }
key
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)