locked
Getting "Parameter count mismatch" Exception. Not adding to Dictionary. what is the problem here? RRS feed

  • Question

  • User339833461 posted

    Hello Everyone,

    I am getting "Parameter count mismatch" error here. Not getting added to dictionary. what is the problem in below code?

    List<Item> items = new List<Item>
                              {
                                 new Item { name = "test1", index = "index1", itemDetails = new ItemProperties{ItemId = "39942dg", Price = 200.00 } },
                                 new Item { name = "test2", index = "index2", itemDetails = new ItemProperties{ItemId = "67942dg", Price = 1000.00 } }
                              };
    
                Dictionary<string, object> dic = new Dictionary<string, object>();
                int i = 0;
                foreach (var itm in items)
                {
                    Dictionary<string, object> dictionary = new Dictionary<string, object>();
                    dictionary.Add("name", itm.name);
                    dictionary.Add("Id", itm.index);
                    if (itm.itemDetails == null)
                    {
                        dictionary.Add("ItemDetails", string.Empty);
                    }
                    else
                    {
                        dictionary.Add("ItemDetails", string.Concat(itm.itemDetails.ItemId + " ", itm.itemDetails.Price.ToString()));
                    }
    
                    dic.Add(i.ToString(), ObjectToString(dictionary));
                    i++;
                }
    
    private static string ObjectToString(object obj)
            {
                Type objType = obj.GetType();
                IList<PropertyInfo> props = new List<PropertyInfo>(objType.GetProperties());
                StringBuilder sb = new StringBuilder(1024);
    
                foreach (PropertyInfo prop in props)
                {
                    string attributeValueString;
                    var type = prop.GetValue(obj, null);
    
                    if (type != null && type.GetType() == typeof(double))
                    {
                        var doubleToStringValue = Convert.ToString(prop.GetValue(obj, null), System.Globalization.CultureInfo.InvariantCulture);
                        attributeValueString = string.Format("\"{0}\":\"{1:0.0}\"", prop.Name, doubleToStringValue);
                    }
                    else if (prop.PropertyType.IsNested)
                    {
                        attributeValueString = string.Format("\"{0}\":{1}", prop.Name, ObjectToString(type));
                    }
                    else
                    {
                        attributeValueString = string.Format("\"{0}\":\"{1}\"", prop.Name, type);
                    }
                    sb.Append(attributeValueString).Append(",");
                }
                return "{" + sb.ToString().TrimEnd(new char[] { ',' }) + "}";
            }
    
     public class Item
        {
            public string name { get; set; }
            public string index { get; set; }
            public ItemProperties itemDetails { get; set; }
        }
    
        public class ItemProperties
        {
            public string ItemId { get; set; }
            public double Price { get; set; }
        }

    Thanks

    Thursday, April 23, 2020 4:34 PM

All replies

  • User475983607 posted

    Can you explain the design intent and the problem you are trying to solve?  Why convert a List<T> into a dictionary of dictionaries?  Are you trying to learn reflection?  Build a custom JSON serializer?  What is the purpose of the code?

    Thursday, April 23, 2020 4:58 PM
  • User339833461 posted

    Here i want only selected item values(name, index). Item values(properties) might be a lot in future like shippingaddress, itemdeliverydate, etc. 

    Thursday, April 23, 2020 5:32 PM
  • User475983607 posted

    Here i want only selected item values(name, index). Item values(properties) might be a lot in future like shippingaddress, itemdeliverydate, etc. 

    Your design does not come close to this requirements.  Can you explain why a List<T> where T is an an address type is not sufficient?

    Thursday, April 23, 2020 5:55 PM