0
When trying to save a change from a client, the SQL Server database is returning the following message: Sqldatetime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
View:
<label>Data de nascimento:</label>
@Html.TextBoxFor(model => model.DataNascimento, new { @class = "form-control col-md-2", type = "date" })
Model: [Required(ErrorMessage = "Por favor, informe uma data de nascimento")]
public DateTime DataNascimento { get; set; }
Controller: [HttpPost]
public IActionResult Editar(EditarClienteModel model)
{
try
{
var cliente = new Cliente();
var clienteRepository = new ClienteRepository();
model.Nome = cliente.Nome;
model.Email = cliente.Email;
model.DataNascimento = cliente.DataNascimento;
clienteRepository.Alterar(cliente);
TempData["Mensagem"] = "Cliente alterado com sucesso";
}
catch(Exception e)
{
TempData["Mensagem"] = "Ocorreu um erro: " + e.Message;
}
return View();
}
Can anyone shed any light on what might be going on? Ps: I’ve tried changing the type of column to datetime2, as I saw in another topic here, but it didn’t work
what has in the method change?
– Lucas Miranda
There’s no reason to change it to
DateTime2
, you have to debug the code and see what is receiving incliente.DataNascimento
– Leandro Angelo
@Lucasmiranda This is the change code, I am using Dapper ORM in this case
public void Alterar(Cliente cliente)
 {
 var query = "update Cliente set Nome = @Nome, Email = @Email, DataNascimento = @DataNascimento" +
 " where IdCliente = @IdCliente";
 
 using (var connection = new SqlConnection(connectionString))
 {
 connection.Execute(query, cliente);
 }
– Pedro Henrique
this code is very strange,it receives a model, instance a client, but does not pass the model data to the client, does the opposite, and tries to save the same client, IE, nothing will be changed there! should be for example
cliente.Nome = model.Nome
and not the other way around!– Ricardo Pontual
@Yes, thanks for the tip, I realized this mistake and changed. However, the reason for the error was that the Datetime input, in the view was receiving some extra information, being necessary to use a
Model.DataNascimento.ToString("yyyy-MM-dd")
After adding this, it worked normally! , there were some other errors in the controller as well, but the reason for this error in question, I believe it was just that same line– Pedro Henrique
the other day Pedro, I don’t know if you’re using any Orms (
entity-framework
,nhibernate
, etc), but if you pass the model data that came from the browser’s direct page to the bank and you don’t have a protection, your code is exposed to "SQL Injection"– Ricardo Pontual
I’m using the ORM Dapper, I don’t know if the way I’m doing my code would be vulnerable, but since you gave me the tip, I’ll research it! obg once again
– Pedro Henrique