Error inserting data in MySql database

0

I get the following error Column 'Nombre' cannot be null when trying to save a record in the database

Private sub Create(ByVal cliente As Cliente)
    Using cn = New MySqlConnection(ConfigurationManager.ConnectionStrings("default").ToString())
        cn.Open()
        Dim cmd as MySqlCommand = cn.CreateCommand()
        cmd.CommandText = "INSERT INTO CLIENTE(nombre, fecha) VALUES(@nombre, DATE_FORMAT(@fecha, '%y%m%d'))"
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@nombre", cliente.Nombre)
        cmd.Parameters.AddWithValue("@fecha", cliente.Fecha)
        cmd.ExecuteNonQuery()
    End Using
End sub

But in the database, it works very well

INSERT INTO CLIENTE(nombre, fecha) VALUES('JOEL', DATE_FORMAT('2017-06-13', '%y%m%d'))
    
asked by Pedro Ávila 13.06.2017 в 15:19
source

1 answer

-4

Why do you mix parameters of a stored procedure with a plain text ???

If you want to put it in text, I would do it like this:

Private sub Create(ByVal cliente As Cliente)
    Using cn = New MySqlConnection(ConfigurationManager.ConnectionStrings("default").ToString())
        cn.Open()
        Dim cmd as MySqlCommand = cn.CreateCommand()
        cmd.CommandText = "INSERT INTO CLIENTE(nombre, fecha) VALUES("+cliente.Nombre+", DATE_FORMAT("+cliente.Fecha+", '%y%m%d'))
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()
    End Using
End sub

In case you want to do it with a procedure: link

    
answered by 13.06.2017 в 17:22