Looks like hf
is null because you're trying to find the HiddenField
using temp.FindControl
instead of temp.Items[count].FindControl
.
Change the following
DropDownList ds = (DropDownList)temp.FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.FindControl("hiddenid");
to this
DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");
UPDATE
This line will throw an error if ds.SelectedValue
is an empty string or contains non numeric characters
user.user_id = int.Parse(ds.SelectedValue);
You need to change it to this
int userID = 0;
if (int.TryParse(ds.SelectedValue, out userID))
{
user.user_id = userID;
}
else
{
// do something when ds.SelectedValue is non numeric
}