BumbleBee
BumbleBee

Reputation: 10779

Error on dropdownlist.SelectedValue

I am loading user control which has a dropdownlist inside it into a page(Mypage). I have set the EnableViewstate to False for the usercontrol. Now the control loads properly and data also is properly bound to the dropdown. On the save event of the page I validate all the controls inside the User control.

                if(int.Parse(ddSuffix.SelectedValue) >= 0)
            {
 Suffix s = new Suffix();
                s.Description = ddSuffix.SelectedItem.Text;
                s.ID = int.Parse(ddSuffix.SelectedValue);
......
}

I get "Input string was not in a correct format." on if(int.Parse(ddSuffix.SelectedValue) >= 0)

P.S : Regardless of I select a value for the dropdown or not the ddsuffix.SelectedValue is "". Is this because I disabled the view state for the usercontrol?

HTML rendered looks like :

<select name="ctl00$DefaultContent$QuoteWizard$Customer1$ddSuffix" id="ctl00_DefaultContent_QuoteWizard_Customer1_ddSuffix" class="TextNormal" style="width:67px;">
                            <option value="0">Select</option>
                            <option value="1">Jr.</option>
                            <option value="2">Sr.</option>
                            <option value="3">II</option>
                            <option value="4">III</option>
                            <option value="5">IV</option>
                            <option value="6">V</option>

                        </select></td>

Thanks in advance

BB

Upvotes: 0

Views: 260

Answers (2)

competent_tech
competent_tech

Reputation: 44931

Change int.Parse to int.TryParse:

int wSelectedValue;

if (int.TryParse(ddSuffix.SelectedValue, out wSelectedValue) {
  if(wSelectedValue >= 0)
  {
    Suffix s = new Suffix();
    s.Description = ddSuffix.SelectedItem.Text;
    s.ID = wSelectedValue;
......
  }
}

Upvotes: 2

Glenn Slaven
Glenn Slaven

Reputation: 34183

I would assume that the ddSuffix.SelectedValue is not a string representation of an integer, can you post the HTML that is rendered for the dropdown list?

Upvotes: 1

Related Questions