Solved

Convert String to Int C#

Posted on 2010-08-27
7
359 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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