Error entering a DateTime data in Visual Studio C #

1

By entering the data in the program in date time format for the database, by accepting it (image 1)

shows me the following error (image 2)

    public bool insertar(string sql)
        {
            cn.Open();
            comando = new SqlCommand(sql, cn);
            int i = comando.ExecuteNonQuery();
            cn.Close();
            if (i > 0)
            {
                return true;
            }
            else
            { return false; }

        }

I put the same time format as it has and asks for the database (image 3).

 private void boton_promocion_Click(object sender, EventArgs e)
    {

        fecha_inicio.Format = DateTimePickerFormat.Custom;
        fecha_inicio.CustomFormat = "dd/MM/yyyy hh:mm:ss tt";
        fecha_fin.Format = DateTimePickerFormat.Custom;
        fecha_fin.CustomFormat = "dd/MM/yyyy hh:mm:ss tt";
        int activo;

        if (radio_promociones.Checked)
        {
            activo = 1;
        }
        else
        {
            activo = 0;
        }

        string registrar_usuario = "insert into tb_promociones(Status, Nombre, Fecha_inicio, Fecha_fin, Descuento, fk_platillos) values ('" + activo + "','" + this.texto_nombrePlatillo.Text + "' , '" + this.fecha_inicio.Text + "','" + this.fecha_fin.Text + "', '" + this.texto_descuento.Text + "', '" + this.combo_platillos.SelectedValue + "')";

        if (obDatos.insertar(registrar_usuario))
        {
            MessageBox.Show("Se ha registrado a Promocion Correctamente");
        }
        else
        {
            MessageBox.Show("Error al Insertar, Consulte al Administrador del Sistema");
        }
    }

CREATE TABLE [dbo].[tb_promociones] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Status] BIT NULL, [Nombre] VARCHAR (50) NULL, [Fecha_inicio] DATETIME NULL, [Fecha_fin] DATETIME NULL, [Descuento] VARCHAR (50) NULL, [fk_platillos] INT NULL, PRIMARY KEY CLUSTERED ([Id] ASC)

insert into tb_promociones(Status, Nombre, Fecha_inicio, Fecha_fin, Descuento, fk_platillos) values ('1','pollo' , '28/05/2018 02:10:02 a. m.','29/05/2018 02:10:02 a. m.', '20', '') 
    
asked by Manuel Barba 28.05.2018 в 10:48
source

1 answer

1

The error you have is that you are passing the date in an incorrect format for the database.

In the Insert that you show us you are passing the '28/05/2018 02:10:02 a. m.' format that is not correct for the Database.

Collecting the dates of the DateTimePicker you should indicate the insertion format to the correct Database, for example "dd/MM/yyyy HH:mm:ss" and it would look like this:

string registrar_usuario = "insert into tb_promociones(Status, Nombre, 
    Fecha_inicio, Fecha_fin, Descuento, fk_platillos) 
    values ('" + activo + "','" + this.texto_nombrePlatillo.Text + "' , '" + 
    this.fecha_inicio.Value.ToString("dd/MM/yyyy HH:mm:ss") + "','" + 
    this.fecha_fin.Value.ToString("dd/MM/yyyy HH:mm:ss") + "', '" + 
    this.texto_descuento.Text + "', '" + this.combo_platillos.SelectedValue + "')";
    
answered by 28.05.2018 / 11:32
source