I have this code:
public JsonResult salvaPaciente(string cns, string sexo, string dataNasc, string nome, string raca,
string cep, string ibge, string logradouro, string endereco, string numero,
string complemento, string bairro, string telefone, string email)
{
byte[] bytes = System.Text.Encoding.GetEncoding("iso-8859-8").GetBytes(nome);
nome = System.Text.Encoding.UTF8.GetString(bytes);
paciente oPaciente = new paciente()
{
bairro = bairro,
cep = cep,
cns = cns,
complemento = complemento,
dataNasc = Convert.ToDateTime(dataNasc),
email = email == "" ? " " : email,
endereco = endereco,
ibge = ibge,
logradouro = logradouro,
nacionalidade = "010",
nomePaciente = nome,
numero = numero,
raca = raca,
sexo = sexo,
telefone = telefone
};
modelOff.pacientes.Add(oPaciente);
try
{
modelOff.SaveChanges();
return Json("sim", JsonRequestBehavior.AllowGet);
}
catch
{
return Json("nao", JsonRequestBehavior.AllowGet);
}
}
The code was working and crash after I actuallize the Visual Studio 2017 and generate the error below:
[FormatException: String was not recognized as a valid DateTime.]
The string dataNasc
contain '24/06/1979'
You can use
DateTime.ParseExact
sample:-
string dataNasc = "24/06/1979";
DateTime time = DateTime.ParseExact(dataNasc, "dd/MM/yyyy", null);
24/06/1979 will not convert so you will need to parse it first.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.