Question

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).

Was it helpful?

Solution 5

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

OTHER TIPS

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.

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

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)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top