0

Hi guys, I'm following an example from a book (incidentally I'm amazed how many mistakes I've found in pretty much all the MVC books I've so far consulted) and I think I've found where the error is by going line by line, compile and executing the app. Basically, this is the controller:

public ActionResult Details(long id = 0)
        {
            var auction = new Auction
            {
                Id = id,
                Title = "Brand new Widget 2.0",
                Description = "This is a brand new version 2.0 Widget!",
                StartPrice = 1.00m,
                CurrentPrice = 13.40m,
               // StartTime = DateTime.Parse("6-15-2012 12:34 PM"),
                //EndTime = DateTime.Parse("6-23-2012 12:34 PM")
            };
            return View(auction);
        }

and here is my model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ebuy.Website.Tests.Models
{
    public class Auction
    {
        public long Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public decimal StartPrice { get; set; }
        public decimal CurrentPrice { get; set; }
        //public DateTime StartTime { get; set; }
        //public DateTime EndTime { get; set; }
    }
}

It all looks OK but it turns out that the error is in the last to lines (here commented out) of the controller:

StartTime = DateTime.Parse("6-15-2012 12:34 PM"),
EndTime = DateTime.Parse("6-23-2012 12:34 PM"),

because when I remove them the application runs OK. Strangely enough Visual Studio doesn't pick up anything but at run time I get this:

String was not recognized as a valid DateTime.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error: 


Line 23:         public ActionResult Details(long id = 0)
Line 24:         {
Line 25:             var auction = new Auction
Line 26:             {
Line 27:                 Id = id,

After having done a lot of testing (as I had absolutely no idea what was going on) and googling, it seems that Date.Parse(string) isn't the right way to go because we need to make sure that whatever date I supply in whichever format can be parsed correctly. Some people seem to say to use ParseExact() but it looks like they are all using it in different ways. So in the end I used these - and the app works:

StartTime = DateTime.Parse( "6-15-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-EN") ),
EndTime = DateTime.Parse( "6-23-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-EN") ),

So my question to you now is: is it really true that any country should have a different set of rules, like GB for Uk etc etc? That's madness! Does it mean that in the real world application developers need to add an awful lot of code to make sure that the app works across the globe?! Or is this just because in this specific case we are hard coding the date?

2
Contributors
7
Replies
25
Views
2 Years
Discussion Span
Last Post by Violet_82
0

Does it mean that in the real world application developers need to add an awful lot of code to make sure that the app works across the globe

Usually, yes. Although the tools we use are providing more and more support. I've worked on a hotel booking system years ago, the server runs in a different country/timezone then the hotel AND the customer, and they all need to see the correct date in their own format, language and timezone.

I haven't done this in MVC yet, but I'm pretty sure this should work without a hassle. Apart from that, you can do:

StartDate = new DateTime(2012, 6, 15, 12, 34, 0);

Edited by pritaeas

0

Oh, that just sounds incredible to me, I mean, I would have thought that the language support would have been more, er, "developed" if you like and provide an easy way to guarantee language support across the globe. Is the problem only with date formats that users input or also with those obtained automatically with, say Date.Now? I presume the latter is standardized? I understand that users could really type anything - although I would have thought that with a bit of code you could change the input to look like the format you need. Am I wrong in thinking that?

0

There are a lot of tools to help recognize dates and times that users input. Dates are more divers than times, as long as you know the user's regional settings, you're good to go. That's why datepickers are so popular ;) you restrict the user in his options (compared to a textbox) and the conversion to what you need is easier.

0

Ah, this is still haunting me...as I am trying to save data into a Entity Framework database, I get this error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

Source Error: 


Line 48:             var db = new EbuyDataContext();
Line 49:             db.Auctions.Add(auction);
Line 50:             db.SaveChanges();
Line 51:             return View(auction);
Line 52:         }

So evidently what I did above isn't the best way forward apparently. How can it possibly go out of range with just dates?
Here is the code that attempts to save the data in the database:

 [HttpGet]
        public ActionResult Create(Auction auction)
        {
            var db = new EbuyDataContext();
            db.Auctions.Add(auction);
            db.SaveChanges();
            return View(auction);
        }

        //
        // POST: /Auction/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }


    }

Edited by Violet_82

0

Thanks, that was useful. I changed from GB to US but alas with no joy. A bit of googling revealed that I could remove the database and create a new one running the application again, but having removed it, even if I run the application again (and with the migration enabled in the package manager console) the database doesn't appear but the darn error is still there. Interestingly enough when I go to the details page of the application I can still see the data displayed like

StartTime
15/06/2012 12:34:00
EndTime
23/06/2012 12:34:00

rather than in the US format, even though in my Auction Controller I have this

StartTime = DateTime.Parse( "6-15-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-US") ),
EndTime = DateTime.Parse( "6-23-2012 12:34 PM", CultureInfo.CreateSpecificCulture("en-US") ),

Edited by Violet_82

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.