Error saving data in SQL Server

2

I have been struggling with this problem for several days to see if they could give me a little help.

I have this error:

  

"Unable to insert the NULL value in the 'CODE' column, table   'C: \ USERS \ MAICOLODALIS \ DOCUMENTS \ VISUAL STUDIO   2010 \ PROJECTS \ SCXP \ SCXP \ BIN \ DEBUG \ SCXP.MDF.dbo.PROVEEDORES '. The   column does not support NULL values. INSERT error. The end of the   instruction. "

this is my code:

    Public Shared Sub AGREGARPROVEEDOR(ByVal codigo As String,
                                   ByVal nombres As String,
                                   ByVal apellidos As String,
                                   ByVal direccion As String,
                                   ByVal ciudad As String,
                                   ByVal telefono As String)
    Using BD As New SCXPEntities1
        BD.PROVEEDORES.AddObject(New PROVEEDORE() With {.CODIGO = codigo,
                                                        .NOMBRES = nombres,
                                                        .APELLIDOS = apellidos,
                                                        .DIRECCION = direccion,
                                                        .CIUDAD = ciudad,
                                                        .TELEFONO = telefono})
        BD.SaveChanges()
    End Using
End Sub

I need your help: (

    
asked by Maicol Odalis 18.04.2016 в 07:28
source

3 answers

4

The problem is that the column código does not support values null , most likely this column is the primary key of your table or a unique key.

What you should do to fix it is assign a value to this column.

Or better yet, before trying to insert the values in the database, check if the assigned values are correct and if not, show the appropriate messages to the user.

Update:

As I said, you have to make sure that the property Código has a value before calling the database to get two things:

  • Preventing the database error (and hiding it from the user because you can uncover details could compromise the security of your application)
  • Show a "friendly" message so that the user understands what is happening and can fix it.
  • This can be implemented in many ways. A simple way to do this could be to convert your method into a function that returns a booleano if the data has been saved and a list of the errors to be displayed to the user.

    The code could be something like (beware that I am from C # and I used the Telerik converter for this code )

    Public Shared Function AGREGARPROVEEDOR(codigo As String, _
        nombres As String, _
        apellidos As String, _
        direccion As String, _
        ciudad As String, _
        telefono As String, _
        ByRef errores As String()) As Boolean
    
        errors = New List(Of String)()
    
        ' Comprobar 
        If String.IsNullOrEmpty(codigo) Then
            errors.Add("Debe establecer el código del proveedor.")
        End If
    
        'TODO: Realizar otras comprobaciones
    
        ' Salir en caso de no pasar las validaciones
        If errors.Count > 0 Then
            Return False
        End If
    
        Using BD As New SCXPEntities1()
            BD.PROVEEDORES.AddObject(New PROVEEDORE() With { _
                Key .CODIGO = codigo, _
                Key .NOMBRES = nombres, _
                Key .APELLIDOS = apellidos, _
                Key .DIRECCION = direccion, _
                Key .CIUDAD = ciudad, _
                Key .TELEFONO = telefono _
            })
            BD.SaveChanges()
        End Using
        Return True
    End Function
    

    Another way to solve it would be using the validation engine provided by the Entity Framework itself as explained in this article (in English)

        
    answered by 18.04.2016 / 08:56
    source
    2

    The same error message describes the situation:

      

    "Unable to insert the NULL value in the 'CODE' column, table   dbo.PROVEEDORES "

    You can modify the 'CODE' column of your SUPPLIERS table so that it accepts null values by means of a script by means of Alter Table .

    Or modify directly in SQL Server the property to accept nulls from your field''CODIGO'' by selecting the attribute " Allow Nulls ":

        
    answered by 18.04.2016 в 17:50
    0

    You could use the example so that you also have an idea for when it does not let you use NULL values, I always do them in the query; Here is an example of how I did it:

    create procedure SP_MANTENMENT_RUTA @ INT OPERATION, @ CODE INT, @Localidad_Rt varchar (150), @Description_Rt varchar (150), @SubRuta bit, @Estate_Rt bit, @LimiteCredit decimal (18.2) ACE BEGIN

    SET NOCOUNT ON;
    -- INSERT
    IF @OPERACION = 1
    BEGIN
    DECLARE @MAXIMO INT
    SET @MAXIMO = (SELECT MAX(ISNULL(Ruta_ID,0)) + 1 AS MAXIMO FROM Logic_Ruta)
        INSERT INTO Logic_Ruta(Ruta_ID,Localidad_Rt, Descripciòn_Rt, SubRuta, Estado_Rt, LimiteCreditRuta)
            values (@MAXIMO,@Localidad_Rt,@Descripcion_Rt,@SubRuta,@Estado_Rt, @LimiteCreditRuta )
    END
    --Actualiza
    IF @OPERACION = 2
    BEGIN
        UPDATE Logic_Ruta SET Localidad_Rt = @Localidad_Rt, Descripciòn_Rt = @Descripcion_Rt,SubRuta =@SubRuta, Estado_Rt = @Estado_Rt, LimiteCreditRuta= @LimiteCreditRuta
            WHERE Ruta_ID = @CODIGO
    END
    --CARGA GRID
    IF @OPERACION = 3
    BEGIN
    SELECT Ruta_ID AS CODIGO,Localidad_Rt AS Localidad_Rt, Descripciòn_Rt AS Descripcion_Rt, SubRuta AS SubRuta, Estado_Rt as Estado_Rt, LimiteCreditRuta AS LimiteCreditRuta
         FROM Logic_Ruta
    END
    

    END

        
    answered by 17.05.2016 в 00:52