I am trying to get data from form collection as:
foreach (string key in col.Keys)// where col is object of form collection
{
if (col["ConstituntName[" + i + "]"].ToString() != null)
{
UserRecordSubClass usr = new UserRecordSubClass();
usr.Value = Convert.ToDecimal(col["ConstituntName[" + i + "]"]);
usr.ConstituentNameId = Convert.ToInt32(col["ConstituntNameId[" + i + "]"]);
}
i++
}
but when the i
is 2, ConstituntName["+i+"]
does not exist so it throws:
System.NullReferenceException.
How can I prevent this exception in that case?