CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2010
    Posts
    24

    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?

  2. #2
    Join Date
    Oct 2010
    Posts
    5

    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

  3. #3
    Join Date
    Sep 2010
    Posts
    24

    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

  4. #4
    Join Date
    Oct 2010
    Posts
    5

    Re: Format Exception

    Strange, mine compiles and runs.

    I put a number in the tb and get a msg for the value.

  5. #5
    Join Date
    Sep 2010
    Posts
    24

    Re: Format Exception

    right. now try it with a null value.

  6. #6
    Join Date
    Oct 2010
    Posts
    5

    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);
            }

  7. #7
    Join Date
    Sep 2010
    Posts
    24

    Smile Re: Format Exception

    Beautiful! Okay that fixed it. Thanks Robert

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Format Exception

    I would have thought that "int?" would have worked....
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Format Exception

    Quote Originally Posted by WileeDarklight View Post
    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?
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured