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.
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.
+ 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)
)
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
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)
0
In other words, the ID and the Id_empleado do other functions in another place
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?