1

when i click button client side date format is working fine but server side date format is different

how to solve this problem?

 [HttpPost]
        public ActionResult CheckAvailabilityValue(MainModel check)
        {

            try
            {
                Get_Location();
                //if (ModelState.IsValid)
                //{
                    locationInformation checking = new locationInformation();
                    bool suc = checking.CheckAvailability(check);
                    if (suc == false)
                    {
                        return Json(new { success = true, message = "Checked successfully" }, JsonRequestBehavior.AllowGet);
                    }
                    else if (suc == true)
                    {
                        return Json(new { False = true, message = "Checked successfully" }, JsonRequestBehavior.AllowGet);
                    }
                //}
            }
            catch
            {
                return View();
            }

            return View();
        }

MainModel Class:

public class CheckingDetails
    {
        [Key]
        public int BookingID { get; set; }
        public int LocationID { get; set; }
        public int FacilityID { get; set; }
        public int VenueID { get; set; }
        public int CapacityID { get; set; }
        public DateTime BookedFromDate { get; set; }
        public DateTime BookedToDate { get; set; }
        public string FromTime { get; set; }
        public string ToTime { get; set; }
    }

Below i attached screen shot

Screen2

Screen1

sereen3

public bool CheckAvailability(MainModel check)
        {
            bool flag = false;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["venue"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("spCheckAvailability", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@BookedFromDate", Convert.ToDateTime(check.CheckMasters.BookedFromDate));
                cmd.Parameters.AddWithValue("@BookedToDate", Convert.ToDateTime(check.CheckMasters.BookedToDate));
                cmd.Parameters.AddWithValue("@FromTime", check.CheckMasters.FromTime);
                cmd.Parameters.AddWithValue("@ToTime", check.CheckMasters.ToTime);
                con.Open();
                flag = Convert.ToBoolean(cmd.ExecuteScalar());
                return flag;

            }
        }
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • Could be the format. Have you tried month/day/year ? – Ric Jun 23 '18 at 06:01
  • no sir@ric ..... – Ivin Raj Jun 23 '18 at 06:06
  • SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. @ric – Ivin Raj Jun 23 '18 at 06:11
  • in client side or server side? @IswarKChettri – Ivin Raj Jun 23 '18 at 06:16
  • When you changed the format did you see the correct value in the debugger? – Ric Jun 23 '18 at 06:21
  • How are you checking the date value in your server side? – Iswar Jun 23 '18 at 06:32
  • data value is different@IswarKChettri – Ivin Raj Jun 23 '18 at 06:34
  • see my screen shot @IswarKChettri – Ivin Raj Jun 23 '18 at 06:37
  • have you tried ` date.toISOString() ` in your client before passing the date value to server? – Iswar Jun 23 '18 at 06:38
  • The value "1/1/0001 12:00:00AM" is the default for a DateTime, so for some reason the values are not even arriving on the server side. – Richardissimo Jun 23 '18 at 06:39
  • yes correct how to solve this issue – Ivin Raj Jun 23 '18 at 06:41
  • what is your client environment? – Iswar Jun 23 '18 at 06:42
  • need to pass date – Ivin Raj Jun 23 '18 at 06:44
  • Some unrelated tips: `SqlCommand` is `IDisposable` so should be in a `using` block. You may want to take a look at [Can we stop using AddWithValue](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/). Also your error message indicates that you're using DateTime as the Sql column type, you may want to consider using DateTime2 (it won't solve your problem), or since those fields are just dates use the Date type. – Richardissimo Jun 23 '18 at 06:46
  • @IvinRaj consider editing the question to make the question clearer, rather than adding to the clutter of these comments. E.g. The question says server side format is different, but that isn't the problem, the problem is that it isn't arriving. – Richardissimo Jun 23 '18 at 06:50
  • It is possible to be a format issue. You can use Data Annotation in model like [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]. For more solution check this question thread. https://stackoverflow.com/questions/7835614/asp-net-mvc3-datetime-format/7836093#7836093 – Karan Jun 23 '18 at 07:33
  • 2
    The format that you send to the controller must match your server culture. If you want to send dates in `dd/MM/yyyy` as your doing then you must change the culture of your server to one that accepts dates in that format (and just use `$('form').serialize()` rather than that unnecessary `GetInfo()` function) –  Jun 23 '18 at 07:44
  • Just to confirm this is a culture issue - do a test and post back some dates where the day is less that or equal to 12 - e.g. 4/6/2018 and 10/6/2018 and confirm its saving (but it will save it as 6th April and 6th October) –  Jun 23 '18 at 07:48
  • What do you mean not working? And what is the culture on your server? –  Jun 23 '18 at 07:52
  • @StephenMuecke what is the culture on your server means? – Ivin Raj Jun 23 '18 at 07:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173666/discussion-between-stephen-muecke-and-ivin-raj). –  Jun 23 '18 at 07:56

0 Answers0