[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Convert String to Int C#

Posted on 2010-08-27
7
Medium Priority
?
372 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 500 total points
ID: 33545553
did you have a space or other character in the selectedValue?
0
 
LVL 16

Assisted Solution

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

Assisted Solution

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

you can try Int.TryParse as well.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 1000 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

Technology Partners: 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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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