converting .text to int

Results 1 to 2 of 2

Thread: converting .text to int

  1. #1
    Join Date
    Dec 1969

    Default converting .text to int

    I&#039;m getting a value (which is always a number) out of a drop down box and trying to convert it into an int. <BR><BR>ActivityRating = DropDownList.SelectedItem.Text; <BR>ActivityRatingInt = Convert.ToInt32(ActivityRating); <BR><BR>The second line gives the error "Input string was not in a correct format. " <BR><BR>I&#039;ve tried ActivityRating = ActivityRating.ToString() and it doesn&#039;t make a difference. Anybody know what I&#039;m doing wrong? <BR><BR>thanks <BR>Dustin

  2. #2
    Join Date
    Dec 1969

    Default Must be a number

    if ActivityRating is blank or contains a character that is not a number, then your error will get fired. this includes any extra spaces or control characters.<BR><BR>I ended up writing a custom method called StringToInt(string sVal) that evaluated the input parameter and converting it to zero before the Convert.ToInt32. Of course, also trapping it to return 0 if the error was fired.<BR><BR>For performance reasons, it is best to try and avoid triggering the error exception.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts