Link to home
Start Free TrialLog in
Avatar of frtools
frtoolsFlag for United States of America

asked on

Convert String to Int C#

I have a drop down list that I need to convert to an integer. Gives me an error "Input string was not in the right format" code is below.

int packID = Convert.ToInt32(DropDownList1.SelectedValue);
ASKER CERTIFIED SOLUTION
Avatar of Stephan
Stephan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of frtools

ASKER

No looks like this:

<asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
        </asp:DropDownList>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of frtools

ASKER

int packID = Convert.ToInt32(DropDownList2.SelectedValue);

Sorry wrong DropDownList......TGIF
That's really strange if it is not working, I have tried your code:

-- front-code --
<asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
        </asp:DropDownList>
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

-- code-behind --
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int packID = Convert.ToInt32(DropDownList2.SelectedValue);
    }

This is working, is there something else that is using this code?