• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

conversion issue string to integer

I have a few sinple lines of code that do not work and I cannot figure out why.  I have a variable that is a string format, and is a number, for example 23.  However when I do an int.Parse(string s) it tells me the input string is not in a correct format, when it clearly is.  The code is simple, and is below:

string text = MessageLabel.Text.Trim();
int projectidnumber = int.Parse(text);
0
zintech
Asked:
zintech
  • 2
2 Solutions
 
käµfm³d 👽Commented:
The only time you should get that message is if you are not passing a valid integer to the Parse method. Have you considered using TryParse instead?

string text = MessageLabel.Text.Trim();
int projectidnumber;

if (int.TryParse(text, out projectidnumber))
{
    // valid [b]int[/b] found, and the value is contained in "projectidnumber"
}

Open in new window

0
 
multithreadingCommented:
If MessageLabel.Text is really "23" this would work. Therefore  it isn't "23". So let's think about what could be going on here.

Maybe you have a Label control and a TexBox control. Maybe the label says something like "Enter 23 right here please:" and the TextBox has the value "23". Maybe what you need is this:

string text = MessageBOX.Text.Trim(); // not the label, but the text box
int projectidnumber = int.Parse(text);

If this isn't it, find out type of object MessageLabel really is and what MessageLabel.Text really is and it will probably lead you straight to the answer.

string text = MessageLabel.Text.Trim();
System.Diagnostics.WriteLine("Text: " + text);
System.Diagnostics.WriteLine("MessageLabel is a " + MessageLabel.GetType().FullName);
int projectidnumber = int.Parse(text);

Open in new window

0
 
zintechAuthor Commented:
I know for a fact that the value I am parsing is an integer.  I set the value of MessageLabel in the OnPreRender() method, and I display that value in the MessageLabel Label on the page, and it displays the integer value.  I will try the TryParse() method now
0
 
zintechAuthor Commented:
Sure enough, 23 was not what was in the Label.  Error on my part.  I had missed that I was informing the user of a successful operation in the same Label as the number, and the text informing the user came AFTER the number was put into the Label.  int.Parse() works just fine.  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now