Data is Null. This method or property cannot be called on Null values

May be related to post Datagrid throws Value cannot be null. (Parameter 'source') - Radzen Studio (Blazor WebAssembly) - Radzen

Adding an SQL Data Source in Studio, then proceeding to write code against it. Very simple -

var products = JNJDatabaseContext.ProductExts.ToList<ProductExt>();

Got the error (see title).

There was a change in EF7 where Null values are not automatically checked (performance gain). The Studio adds the required Null operator

float? MyDatabaseField {get; set;}

to model properties that allow Null and are not strings.
But the problem is with strings that allow Nulls. The Data Wizard doesn't add the ? on to string properties. When I manually added it to all Nullable strings, it worked as expected.

Paul

Hi @Paul_Ruston,

Can you post the exception stack trace? Iā€™m not sure also where you added the null check operator.

Hi @enchev

While running / debugging, it didn't report anything. It didn't crash out or show any error message. It just didn't show me any data. It was when I was analysing the variable and forcing the results to evaluate, that it showed me the error, shown in Results View -> Message.

The null operator is placed on the type in the EF classes in the Models folder. i.e.
these two properties,

    public string Type { get; set; }
    public string Image { get; set; }

would become

    public string? Type { get; set; }
    public string? Image { get; set; }

Just as an addition, when reverting my code to force the error, I changed one of the properties and it still ran OK. When I looked at the definition in SQL Server, the field was defined as nvarchar(MAX). So I changed another property and then got the error back. That was defined as nvarchar(50)

Hope this helps

Paul

EDIT - My bad on the wording. It's not Null operator, it's Nullable type.

Hi @Paul_Ruston,

Please post the SQL schema of the problematic table to try to reproduce the exception locally.

Hi @enchev

Here is the schema for the table in question.

CREATE TABLE [dbo].[ProductExts](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[StoreId] [bigint] NOT NULL,
[ShopifyProductRef] [bigint] NOT NULL,
[IncludeInApp] [bit] NOT NULL,
[Title] nvarchar NOT NULL,
[Description] nvarchar NULL,
[Type] nvarchar NULL,
[Image] nvarchar NULL,
[Markup] [decimal](18, 2) NOT NULL,
[ApplyVariantSizeCalcs] [bit] NOT NULL,
[VariantConstructionCost] [decimal](18, 2) NOT NULL,
[VariantModifier] [decimal](18, 2) NOT NULL,
[MinVariantPrice] [decimal](18, 2) NOT NULL,
[MaxVariantPrice] [decimal](18, 2) NOT NULL,
[CurrentMaxPrice] [decimal](18, 2) NOT NULL,
[DiffCost] [decimal](18, 2) NOT NULL,
[DiffPc] [decimal](18, 2) NOT NULL,
[ProcessType] nvarchar NOT NULL,
[Calculated] [bit] NOT NULL,
[HtmlVariants] nvarchar NOT NULL,
[StringVariants] nvarchar NOT NULL,
[ShopifyStatus] nvarchar NOT NULL,
[ManualUpload] [bit] NOT NULL,
[LastCalculated] [datetime] NULL,
[LastUpload] [datetime] NULL,
[IsVariant] [bit] NOT NULL,
[Handle] nvarchar NOT NULL,
[InProcess] [bit] NOT NULL,
[Uploaded] [bit] NOT NULL,
[Collection] nvarchar NOT NULL,
[VariantOption1] nvarchar NULL,
[VariantOption2] nvarchar NULL,
[VariantOption3] nvarchar NULL,
[IsQuote] [bit] NULL,
[QuoteDate] [datetime] NULL,
[ValidUntil] [date] NULL,
[ShopifyCustomerId] [bigint] NULL,
[LineItemDescription] nvarchar NULL,
[ShopifyOrderId] [bigint] NULL,
[MaterialProfileComplete] [bit] NULL,
[MaterialProfileError] nvarchar NULL,
[TaskProfileComplete] [bit] NULL,
[TaskProfileError] nvarchar NULL,
[Duration] [float] NULL,
[WizSupplier] [bigint] NULL,
[Text01] nvarchar NULL,
[Text02] nvarchar NULL,
[Number01] [bigint] NULL,
[Number02] [bigint] NULL,
CONSTRAINT [PK_ProductExts] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_IncludeInApp] DEFAULT ((0)) FOR [IncludeInApp]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Markup] DEFAULT ((0.0)) FOR [Markup]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_VariantConstructionCost] DEFAULT ((0.0)) FOR [VariantConstructionCost]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_VariantModifier] DEFAULT ((0.0)) FOR [VariantModifier]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_FixedConstructionCost] DEFAULT ((0.0)) FOR [MinVariantPrice]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_SuggestedPrice] DEFAULT ((0.0)) FOR [MaxVariantPrice]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_CurrentPrice] DEFAULT ((0.0)) FOR [CurrentMaxPrice]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_DiffCost] DEFAULT ((0.0)) FOR [DiffCost]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_DiffPc] DEFAULT ((0.0)) FOR [DiffPc]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Calculated] DEFAULT ((0)) FOR [Calculated]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_HtmlVariants] DEFAULT (N'Not Calculated') FOR [HtmlVariants]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_StringVariants] DEFAULT ('') FOR [StringVariants]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Status] DEFAULT ('') FOR [ShopifyStatus]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_ManualUpload] DEFAULT ((0)) FOR [ManualUpload]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_IsVariant] DEFAULT ((0)) FOR [IsVariant]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Handle] DEFAULT ('') FOR [Handle]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_InProcess] DEFAULT ((0)) FOR [InProcess]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Uploaded] DEFAULT ((0)) FOR [Uploaded]
GO

ALTER TABLE [dbo].[ProductExts] ADD CONSTRAINT [DF_ProductExts_Collection] DEFAULT ('') FOR [Collection]
GO

Additional

I created a quick mock up with null nvarchar fields both MAX and limited and couldn't recreate the problem. So there is probably something else that's contributing to the error. But it was definitely solved and recreated with this schema by adding or removing the Nullable type to the model.

Regards

Paul

Hi @enchev

I don't think you should waste your time on this. The error is real, but I don't know why it's happening in that one application.

In the mock up I mentioned above, I added the ProductExt table and carried out a basic -

var results = PRDatabaseContext.ProductExts.ToList<ProductExt>();

and it worked. Baring in mind, I have not added any Nullable types yet.

But if I go back to my original application, I can reproduce the error but there's no pattern. I have just gone through every string field individually in ProductExt model, removed the Nullable type and run the application. It fails when the Nullable type has been removed from either -

Image
LineItemDescription
MaterialProfileError
TaskProfileError
Text01
Text02

But there are other string properties that don't fail. I have looked at the data in the table and can see no pattern as to why this is happening.

I'll keep digging and see if anything comes to light.

Many thanks

Paul

Could it be related to the following setting in the .csproj file?

<Nullable>enable</Nullable>

It isn't present by default in applications created by RBS.

1 Like

Hi @korchev

*** Excellent ***

That's the one. I was sceptical at first as it seemed to be an EF problem, there being no pattern as to which Null properties failed and which didn't. But this as sorted it!

For anyone reading this thread in the future, I got this problem by creating my project in Visual Studio 2022, and then, at a later date, used Blazor Studio to create a set of data files (Models, Context and Services).

Visual Studio defaults a setting in the project file <Nullable>enable</Nullable>.
Removing this setting sorted the problem. If you prefer to have this active, you may have to review all the string properties in your Models and add the Nullable (string?) character to them.

Thanks @korchev