-
October 4th, 2010, 02:31 PM
#1
Format Exception
I get "Input string was not in a correct format." on the following when attempting to pass a null value:
ProductType = new clsProductType();
ProductType.SortOrder = Int32.Parse(txtSortOrder.Text); <----- This Line.
//ProductType.SortOrder = Convert.ToInt32(txtSortOrder.Text); <----- Also tried this
Sort Order is declared as such:
public int? SortOrder { get; set; }
In the constructor for the class i have SortOrder = null, and elsewhere in the code have it take a number from a textbox
SortOrder is used to pass a variable so that it can be written to an SQL Table. The value needs to accept an integer but can also be null-able.
Any Ideas?
-
October 4th, 2010, 03:12 PM
#2
Re: Format Exception
VS 2008
Just guessing here, but have you tried:
Here is my class:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class clsProductType
{
private int sortorder;
public clsProductType()
{
// sortorder = null; <<--- this won't compile for me
}
public int SortOrder
{
get
{
return sortorder;
}
set
{
sortorder = value;
}
}
}
}
Here is the calling code: (WinForm)
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
clsProductType ProductType = new clsProductType();
ProductType.SortOrder = System.Convert.ToInt32(this.textBox1.Text);
MessageBox.Show("SortOrder = " + ProductType.SortOrder);
}
}
Does this help you?
robert
-
October 4th, 2010, 03:28 PM
#3
Re: Format Exception
Hey Rob,
Well first off, yours won't compile because you don't have a set + get for that declaration.
And secondly, no that didn't do anything different. Same error. Thanks for your input though!
P.S. My problem persists if anyone would like to give some pointers
-
October 4th, 2010, 03:30 PM
#4
Re: Format Exception
Strange, mine compiles and runs.
I put a number in the tb and get a msg for the value.
-
October 4th, 2010, 03:34 PM
#5
Re: Format Exception
right. now try it with a null value.
-
October 4th, 2010, 03:50 PM
#6
Re: Format Exception
k, now I see.
Since the int is not nullable, there needs to be a value you can set to signify that no sort has been specified. Like zero or -1 or 999 or some integer and trap it in the catch:
Code:
private void button2_Click(object sender, EventArgs e)
{
const int NOSORTSPECIFIED = 0;
clsProductType ProductType = new clsProductType();
try
{
ProductType.SortOrder = Convert.ToInt32(this.textBox1.Text);
}
catch (FormatException exf)
{
ProductType.SortOrder = NOSORTSPECIFIED;
}
MessageBox.Show("SortOrder = " + ProductType.SortOrder);
}
-
October 4th, 2010, 03:54 PM
#7
Re: Format Exception
Beautiful! Okay that fixed it. Thanks Robert
-
October 5th, 2010, 01:11 AM
#8
Re: Format Exception
I would have thought that "int?" would have worked....
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
October 5th, 2010, 02:22 PM
#9
Re: Format Exception
Originally Posted by WileeDarklight
I get "Input string was not in a correct format." on the following when attempting to pass a null value:
ProductType = new clsProductType();
ProductType.SortOrder = Int32.Parse(txtSortOrder.Text); <----- This Line.
[..]
Isn't that simply down to what is in the text box?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|