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.

Dinesh
  • 507
  • 4
  • 14
  • 23
  • Do you know what's stored in `TaskText.Text` and `ProjectText.Text`? – Kamil Budziewski Jul 22 '13 at 10:39
  • What will `TaskText.Text` and `ProjectText.Text` contain? – Andy Jul 22 '13 at 10:40
  • I'll add string values to it at runtime. – Dinesh Jul 22 '13 at 10:41
  • 1
    I would make `task` and `project`(consider to follow .NET naming conventions, properties should be uppercased) to be `Nullable` instead. So if the input is invalid you could return `null`. – Tim Schmelter Jul 22 '13 at 10:41
  • But to pass these values to that constructor, I need these values to be integer. – Dinesh Jul 22 '13 at 10:42
  • Please be clear on what values you are expecting in `TaskText.Text` and `ProjectText.Text`. From your comments on the answers, it seems you are trying to use `Parse` for something other than what it is intended for. – Vanlalhriata Jul 22 '13 at 11:01
  • I want to enter string values to TaskText.Text and ProjectText.Text at runtime. But when I pass these to that constructor, I want to pass those values as int values. Is it possible to do? – Dinesh Jul 22 '13 at 11:05

4 Answers4

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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
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.

Vanlalhriata
  • 559
  • 5
  • 19
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);
blindmeis
  • 22,175
  • 7
  • 55
  • 74
-1

You must check if you can parse it into Integer

try

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