0

Input string was not in correct form. I'm getting an exception on runtime as "System.FormatException".

Follwing lines shows exception-

public int Task 
{
    get
    { 
        return Int32.Parse(TaskText.Text);
    }
    set
    { 
        TaskText.Text = value.ToString(); 
    }
}

public int Project 
{
    get
    { 
        return Int32.Parse(ProjectText.Text);
    }
    set
    { 
        ProjectText.Text = value.ToString(); 
    }
}

I also tried -

Convert.ToInt32(TaskText.Text)
Convert.ToInt32(ProjectText.Text)

I need to pass these to following constructor,

Harvest_TimeSheetEntry entry = new Harvest_TimeSheetEntry(client,starttime,stoptime,task,project);

this constructor is stored in some class with task and project as integer parameters. And I can't change it because if i changed, it affects other code.

7
  • Do you know what's stored in TaskText.Text and ProjectText.Text? Commented Jul 22, 2013 at 10:39
  • What will TaskText.Text and ProjectText.Text contain? Commented Jul 22, 2013 at 10:40
  • I'll add string values to it at runtime. Commented Jul 22, 2013 at 10:41
  • 1
    I would make task and project(consider to follow .NET naming conventions, properties should be uppercased) to be Nullable<int> instead. So if the input is invalid you could return null. Commented Jul 22, 2013 at 10:41
  • But to pass these values to that constructor, I need these values to be integer. Commented Jul 22, 2013 at 10:42

4 Answers 4

3

It looks as though you're getting your input from controls accepting user input, which is just asking for failure, since a user can potentially enter something that doesn't represent an integer value. You can use TryParse to avoid this:

var result = 0;
if (int.TryParse(TaskText.Text, out result)) {
  return result;
}
return 0;

So, if the value of TaskText.Text == "1", this will succeed; if the value of TaskText.Text == "aaaa", this will fail - and return zero. You example would raise the appropriate exception, as experienced.

However, an exception might be the right thing to happen here, if you can't handle a bad value, don't have an alternative, and the application relies on the input to move forward. More likely, you could do with some validation on your input fields to prevent bad data being submitted.

Sign up to request clarification or add additional context in comments.

Comments

0

Since your Harvest_TimeSheetEntry constructor expects task and project to be integers, you must have a list of integers that correspond to the different tasks and projects. Now you can't expect Int32 to know which task corresponds to which number, can you?

I would suggest you use ComboBoxes for TaskText and ProjectText. Then, you can assign the correct corresponding integer to each ComboBoxItem.Tag.

Please note that this goes far beyond the kind of answers you should expect from SO.

3 Comments

Ok. I'll try by this way.
I tried by using comboboxes, but it still shows the same exception.
This is a classic case of shotgun debugging. Once again, you are getting the exception because of the way you are trying to use Parse. Have you even looked it up?
0

if you do not use MVVM or binding you can simply do the check before your need it. t

 int task;
 int project;

 if(!Int32.TryParse(TaskText.Text, out task))
 {}       //errorhandling here

 if(!Int32.TryParse(ProjectText.Text, out project))
 {}//errorhandling here

 //all fine
 var entry = new Harvest_TimeSheetEntry(client,starttime,stoptime,task,project);

Comments

-1

You must check if you can parse it into Integer

try

Int32 foo =0;
if (Int32.TryParse(TaskText.Text, out foo))
{
    return foo;
}

1 Comment

If it parses, foo will contain the value, no need to then call convert too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.