0

I have this application that automatically generates order ID. Now I want it to be like this 9999-99-9999, the first four numbers represents the Year. Now, the second one is the month and the last is auto-incremented.

I'm working now on the second one which is the month. So in my code I have to know whether it is a two digit number or one digit number so that I can add a zero. When I run the application there is an error which says input string was not in a correct format. What does it mean? Is there an error in my code?

private void btnOk_Click(object sender, EventArgs e)
{
    string order_ID = DateTime.Now.Year.ToString();
    order_ID += "-";

    if (Convert.ToInt32(order_ID) < 10)
    {
        order_ID += "0";
    }

    order_ID += DateTime.Now.Month.ToString();

}

The error is in this line Convert.ToInt32(order_ID).

3
  • order_ID += "-"; What is the purpose of this line of code? i think this is the problem. Commented May 15, 2013 at 9:11
  • 2
    Why would you expect a string of "2013-" to be parsable as an integer? And why don't you just use DateTime.Now.ToString("yyyy-MM")? Commented May 15, 2013 at 9:11
  • (Note that you need the invariant culture - please don't take my exact code from that comment.) Commented May 15, 2013 at 9:20

5 Answers 5

5

It's really not clear what you're trying to achieve with this - why would you expect a parsed year to ever be less than 10? Unless you're really expecting some seriously broken system clocks, there's no need for this.

But you don't need to convert the year and month separately at all:

string orderId = DateTime.UtcNow.ToString("yyyy-MM",
                                          CultureInfo.InvariantCulture);

Note the use of UtcNow as a generally better idea than using the system local time zone - though you can change that if you really to want to use the system local time zone.

The use of CultureInfo.InvariantCulture will avoid it converting to a different calendar system if the current culture's default calendar system is non-Gregorian, and also avoid using non-invariant formatting information. (The latter wouldn't be an issue in this case, but can easily be an issue with other format strings.)

You'd then need to add the auto-increment part, of course, but that's a different matter.

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

1 Comment

Thank you Sir now I've realized my mistake. :)
1

The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer. You can use Int.TryParse instead

Comments

1

The fault is you are using a dash...

order_ID += "-";

...and then you are trying to convert it to an int

    if (Convert.ToInt32(order_ID) < 10)

You can't make this an integer. You may have more luck with the TryParse() method.

So, you need to re-jumble your code a little.

Either add the - at a different time, or remove it when converting to an int

    if (Convert.ToInt32(order_ID.Replace("-","") < 10)

Comments

1

For your code :

private void btnOk_Click(object sender, EventArgs e)
{
    string order_ID = DateTime.Now.Year.ToString();

    if (DateTime.Now.Month < 10)
    {
        order_ID += "0";
    }  
    order_ID += "-"; 

    order_ID += DateTime.Now.Month.ToString();

}

But Mehran'solution is easier and most elegant.

Comments

0

seems The program has failed converting your order_ID to an integer because it contains - ,

you actually don't need to do that just use this :

string order_ID = DateTime.Now.ToString("yyyy-MM");

4 Comments

It's not the compiler doing this. And note that it's worth including the invariant culture, for the reasons I talked about in my answer.
But Sir I have another question how can I do the auto-increment thing? Integrating it into the order_ID string?
probably you can have an Integer called count and every time add to order_ID , order_ID += count.ToString();
thanks @JonSkeet , I'll keep that in mind , I'm pretty new in c# and programming and have a lot to learn :)

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.