L
Learning Rocks
Guest
Hello Everyone,
I am getting "Parameter count mismatch" error here. 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
Continue reading...
I am getting "Parameter count mismatch" error here. 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
Continue reading...