0

The code is working perfectly on my local computer. I built the solution, I moved it to virtual machine, and there it is triggering me the error unhandled system data client exception the conversion of a varchar data type to a datetime type resulted in a out-of-range?

It is a Windows virtual machine. I thinks the error is triggered by that DateTime.Now but I do not realise why, because on local works.

Status status = new Status()
{
    Id= 20,
    CreatedAt = DateTime.Now,
    Operation = "getpayload",
    Payload = JsonConvert.SerializeObject(payload)
};
Query.InsertStatus(status);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Paul Viorel
  • 234
  • 1
  • 11
  • 1
    The question is, what does the `Query.InsertStatus` method do? This code snippet looks okay so far. I suspect it is constructing a SQL string with string concatenation instead of using command parameters. – Olivier Jacot-Descombes Jan 25 '23 at 11:57
  • 2
    It works for you because your machine has different regional settings. Somewhere a date/time value that should have remained a date/time value is being converted to a string, in a culture-dependent manner. The solution is usually to eliminate such a conversion altogether. – Jeroen Mostert Jan 25 '23 at 12:00
  • That method insert into database `status` object. The table from database looks like `id (bigint not null), CreatedAt (datetime, not null), Operation (nvarcahr(255), not null), Payload(navarchar(max) not null)`. – Paul Viorel Jan 25 '23 at 12:02
  • The problem is most likely in the `Query.InsertStatus` method. Please add it to your question. – Olivier Jacot-Descombes Jan 25 '23 at 12:03
  • @JeroenMostert you were right. I updated the regional settings as they are on my local and now it works. Thanks – Paul Viorel Jan 25 '23 at 12:06
  • @OlivierJacot-Descombes thanks for trying to help me, that line of code is good. The problem was with regional settigs. – Paul Viorel Jan 25 '23 at 12:07
  • 2
    Changing your settings may make it "work", but your code is still deeply flawed and subject to nasty things like [SQL injection](https://stackoverflow.com/q/601300/4137916), as well as more basic issues with `NULL` values and innocent strings that contain single quotes. Read up on how to execute queries with parameters. Do *not* build statements with string interpolation. – Jeroen Mostert Jan 25 '23 at 12:07
  • See also: [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements). There are also answers showing how to use parametrized queries. – Olivier Jacot-Descombes Jan 25 '23 at 12:36

0 Answers0