How can I store the date? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I store the date?

I am making an application in C# with Visual Studio and SQL server and at the time of saving the logs in the windowns form tells me: "error of conversion to convert the value to varchar 12-02-2019 in a data int. Here the code: SqlCommand otroCmd = new SqlCommand("INSERT INTO Salidas values ('" + txtmotivo.Text + "','" + txttipo.Text+ "','" + dtsalida.Text + "','" + cbempleado.SelectedValue + "')", conexion); otroCmd.ExecuteNonQuery(); conexion.Close(); Clarified that the date I have saved as date in sql server and I am using the stored procedure. And the dtsalida is the date time picker.

7th Apr 2019, 10:36 PM
Dante M
6 Answers
+ 1
Could you share the 'Salidas' table structure here? or the stored procedure? I wonder because of that conversion failure message. I noticed all the values you put in your query are wrapped in single quotes, I assume all those fields' type were varchar or char? Are the order of the values correctly sequenced? I ask this because I see you didn't specify the field names in the query. Try to display the query with a message box or something, and carefully observe for any miss. Never mind if you have solved it, it's been hours anyways. But it would be nice if you mark the post with [SOLVED] just to avoid misunderstanding.
8th Apr 2019, 9:44 AM
Ipang
+ 1
Look my table: create table Salida( id int primary key identity, motivo varchar(60) not null, tipo varchar(10) not null, id_empleado int not null, fecha date not null constraint FK_claveEmpleados Foreign Key(id_empleado) references Empleados(id) )
8th Apr 2019, 10:37 AM
Dante M
0
Dante M, Try to execute this query: SqlCommand otroCmd = new SqlCommand("INSERT INTO Salidas (motivo, tipo, id_empleado, fetcha) values ('" + txtmotivo.Text + "','" + txttipo.Text + "'," + cbempleado.SelectedValue + ",'" + dtsalida.Text + "'"), conexion); Explanation: 1. It is recommended to explicitly specify the field names when inserting data to a table that uses identity field type as primary key. System automatically assign a value for identity field, as such, we better specify the field names along with the values to insert, excluding the identity field. 2. The error happened because the values in the query didn't come in sequence. The values don't match the sequence of field in the structure. The conversion failed when trying to convert the cbempleado.SelectedValue (int) into `fetcha` field (date). Hth, cmiiw
8th Apr 2019, 11:16 AM
Ipang
0
Excuse me, but in my program does not use the id and the id_employee because the I have for me to meet internal functions and it is not necessary to make a text box with these two already that when you click on the button to enter automatically stores a id and the combo box (cbempleados) shows the ID of the user who I am saving (in this case by removing the system)
8th Apr 2019, 11:33 AM
Dante M
0
In other words, the ID and the Id_empleado do other functions in another place
8th Apr 2019, 11:36 AM
Dante M
0
Dante M, I don't understand what you mean with internal functions. Did the query succeeded to run or not? did it solve the problem with conversion failure?
8th Apr 2019, 2:57 PM
Ipang