1
I have a program in c# (Visual Studio) of registration that contains a textbox to receive a date. Knowing that SQL records the date in American format, how can I make the user to enter the date in Brazilian format and this date can be saved successfully in the database? As I am doing, the user type the date in the Brazilian format, but an error occurs when saving, because SQL is trying to save MM/dd/yyyy. Could someone help me, please? I thank you in advance.
Below the code I’m using:
//Variável que recebe o cast no textbox
var validaDataVenda = DateTime.Parse(txtDataVenda.Text);
var validaDataPagto = DateTime.Parse(txtDataPagamento.Text);
//Comando que insere os dados no banco, recebendo os textboxes como parâmetros
VendaDAO.InsertVenda(novoCodigo.ToString(), txtValor.Text, dataDaVenda.ToString(), dataDoPagamento.ToString(), txtCodigoCliente.SelectedValue.ToString(), txtCodigoProduto.SelectedValue.ToString(), txtQuantidadeRequerida.Text, recebido);
If I insert "31/102016", for example, this error occurs: "Conversion failed when Converting date and/or time from Character string".
How is the connection between the application and SQL being made? Entity Framework? ADO.NET?
– André Figueiredo
When you pass the date to the routine you insert into the database, you use the textbox string directly or cast to datetime?
– Intruso
@Andréfigueiredo ADO.NET.
– Cristian Quadros
@Intruder I do straight from the textbox.
– Cristian Quadros
Cast to datetime and pass the datetime to the parameter that will be used in Insert/update. Storage (db) is storage, GUI and formatting do not need to be represented in the same way in db.
– Intruso
@Intruder keeps giving error :( The problem is that sql only accepts if it is MM/dd/yyyy. But the user will type dd/mm/yyyy in the textbox.
– Cristian Quadros
Paste the code that takes the value of the screen and the code that makes the Insert here. Also put the error that SQL returns, the value that represents the date of a Datetime is internal and does not depend on the formatting. If you could cast it, it should work.
– Intruso
//Variável que recebe o cast no textbox
var validaDataVenda = DateTime.Parse(txtDataVenda.Text);
var validaDataPagto = DateTime.Parse(txtDataPagamento.Text);

//Insere no banco recebendo os textbox como params
VendaDAO.InsertVenda(novoCodigo.ToString(), txtValor.Text, dataDaVenda.ToString(), dataDoPagamento.ToString(), txtCodigoCliente.SelectedValue.ToString(), txtCodigoProduto.SelectedValue.ToString(), txtQuantidadeRequerida.Text, recebido);

Se eu inserir "31/102016", por exemplo, ocorre este erro:
"Conversion failed when Converting date and/or time from Character string".
– Cristian Quadros
@Intruder I edited my question to improve the visualization of the code.
– Cristian Quadros
Why do you need to do dataDaVenda.Tostring() ? You test if the field value is null before casting?
– Intruso