Take the 2-minute tour ×

I am having a problem with data type conversion that I just can figure out

 HiddenField hfFriendId = item.FindControl("hfFriendId") as HiddenField;
 Int32 FriendId = Convert.ToInt32(hfFriendId.Value);

I need to use FriendId as and int32 in the following

UserInfo TargetUser = UserController.GetUserById(PortalSettings.PortalId, FriendId);

as both args are expected to be int32. When I debug this 'TargetUser' is null.

What might the problem be?

share|improve this question
Is there any UserInfo available for the parameters passed ? What is the value of FriendId when you pass the argument ? –  V4Vendetta Oct 4 '12 at 5:20
check if the value of hfFriendId.Value is empty (or null) or not. Int32 FriendId=0; if(hfFriendId.Value!=""){ FriendId = Convert.ToInt32(hfFriendId.Value); else {FriendId =0;}} –  user1509 Oct 4 '12 at 5:22
That's the thing, FriendId it is populated with a value –  Mark Hollas Oct 4 '12 at 5:24
Is the name of the field correct (hfFriendId)? –  Tim Oct 4 '12 at 5:25
watching hfFriendId in debug I can see it has value. also when I look at UserInfo TargetUser = UserController.GetUserById(PortalSettings.PortalId, FriendId); I can see there that FriendId has a value but in the eind TargetUser remains null –  Mark Hollas Oct 4 '12 at 6:02

1 Answer 1

up vote 0 down vote accepted

If you are not sure of the value available in the hiddenfield then instead of using

Int32 FriendId = Convert.ToInt32(hfFriendId.Value);


int FriendId = int.Parse(hfFriendId.Value); // will throw exception if parse fails

or Better

int FriendId ;
if(int.TryParse(hfFriendId.Value, out FriendId))
     //get userinfo

Also it could be an issue with your UserController.GetUserById method in case the Convert gets the value parsed and is not returning zero.

share|improve this answer
I tried both these suggestion but can not achieve a result.. –  Mark Hollas Oct 4 '12 at 6:07
I am using a hidden field in a asp:listView itemtemplate to pass value back to the code behind. HiddenField by nature is a string. I wonder, is there similar way I could pass the value back as an int? –  Mark Hollas Oct 4 '12 at 6:08
What you suggested is a valid answer to my question. The issue however was self produced. the UserInfo object needs not just an int data type but also a valid userid! Somehow my test data was item was not a valid user id. –  Mark Hollas Oct 4 '12 at 7:45

Your Answer


