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);
zintechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.