I had to work a very simple implementation for a dot net page where on the dropdownlist I had to bind string values from ArrayList and Hashtable.
When the data was loaded from hash table and then from the ArrayList was the situation to got this error.
The reason behind is, while using the Hashtable, I has assigned for key and value to be populated from the hash table, this said, when I populate it from the ArrayList, the control tries to assign value and key from the object inside the ArrayList which was plain String which doesn’t have value property.
Simple remedy:
1. Assign the key and values to null on the case of the ArrayList OR
2. Wrap the string other object and provide a property of Value – I haven’t tried it, but in principle, it should work.
Here is a snippet of the code behind:
-
protected void btnArrayList_Click(object sender, EventArgs e)
-
{
-
list.Add("Name one");
-
list.Add("Name two");
-
list.Add("Name three");
-
list.Add("Name four");
-
DropDownList1.DataSource = list;
-
DropDownList1.DataTextField = null;
-
DropDownList1.DataValueField = null;
-
DropDownList1.DataBind();
-
}
-
-
protected void btnHashTable_Click(object sender, EventArgs e)
-
{
-
table.Add("one", "name one");
-
table.Add("two", "name two");
-
table.Add("three", "name three");
-
table.Add("four", "name four");
-
table.Add("five", "name five");
-
DropDownList1.DataSource = table;
-
DropDownList1.DataTextField = "Value";
-
DropDownList1.DataValueField = "Key";
-
DropDownList1.DataBind();
-
}
-
Happy DataBinding.