Solved

Convert String to Int C#

Posted on 2010-08-27
7
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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