lunes, 25 de julio de 2011

A null value not allowed has been selected in the database.

I'm getting the error:
No se puede seleccionar un registro en Proyectos (ProjTable).
Se ha seleccionado un valor NULL no permitido en la base de datos.
When we have a null value selected from the database, the table buffer is unable to show the values to the form, nor the table view in the AOT. Here is what I found in SQLServer:
In my case the answer was easy - these are string fields so I could edit them directly in the db.
UPDATE [DynamicsAXDev].[dbo].[PROJTABLE]
   SET [EVE_PROJWFSENTCOMMENT] = ''
 WHERE [EVE_PROJWFSENTCOMMENT] is null;
I'm wondering if it was because I'm importing records using an 'old' ProjTable as my colleague informs me that he added a new field to the table recently. 

If you have a large table with both many columns and rows then you will need to use some SQL and Select columns with NULL values only. I'll copy the code here to make me look intelligent:
declare @col varchar(255), @cmd varchar(max) 
DECLARE @TableName Varchar(100) 
SET @TableName='ProjTable' 

DECLARE getinfo cursor for 
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID 
WHERE t.Name = @TableName 
 
OPEN getinfo 
 
FETCH NEXT FROM getinfo into @col 
 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    SELECT @cmd = 'IF NOT EXISTS (SELECT top 1 * FROM ' + @TableName + ' WHERE [' + @col + '] IS NOT NULL) BEGIN print ''' + @col + ''' end' 
    EXEC(@cmd) 
 
    FETCH NEXT FROM getinfo into @col 
END 
 
CLOSE getinfo 
DEALLOCATE getinfo
Do check out MSDN's Null Values for Data Types article as to what to insert into these evil null value fields when they appear.

Even after performing all of the above I was still receiving the error message. It was time to close and reopen my Ax client.

No hay comentarios:

Publicar un comentario