Solved

Convert String to Int C#

Posted on 2010-08-27
7
347 Views
Last Modified: 2013-12-17
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);
0
Comment
Question by:frtools
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 16

Accepted Solution

by:
Stephan earned 125 total points
ID: 33545553
did you have a space or other character in the selectedValue?
0
 
LVL 16

Assisted Solution

by:SriVaddadi
SriVaddadi earned 250 total points
ID: 33545572
use
int.TryParse(DropDownList1.Text,out packId)
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 125 total points
ID: 33545577
what is the type and value of SelectedValue

you can try Int.TryParse as well.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:frtools
ID: 33545579
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>
0
 
LVL 16

Assisted Solution

by:SriVaddadi
SriVaddadi earned 250 total points
ID: 33545594
int.TryParse(DropDownList1.Text,out packId) should be working
0
 

Author Closing Comment

by:frtools
ID: 33545622
int packID = Convert.ToInt32(DropDownList2.SelectedValue);

Sorry wrong DropDownList......TGIF
0
 
LVL 16

Expert Comment

by:Stephan
ID: 33545643
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?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now