D3bug
D3bug

Reputation: 81

Cannot insert nullable value in sqlite db

I'm using sqlite and I'm trying to insert a DateTime value, now happen in some case that the value's null and I get this warning:

nullable object must have a value

the code that cause the fault is this:

command.Parameters.AddWithValue("Date", item.Start?.DateTime.Value.Date.ToString("yyyy-MM-dd"));

command is the SQLiteCommand and item is a Google Calendar Event (I'm using the Google Calendar API).

How you can see I've tried to fix declaring Start as nullable with ? but I got the same problem.

The value of Start is this: {Google.Apis.Calendar.v3.Data.EventDateTime}

This is an image of the properties contained into .Start:

Test

How can I fix this?

Upvotes: 1

Views: 90

Answers (1)

Shachaf.Gortler
Shachaf.Gortler

Reputation: 5735

In that case the object is C# null you should use DB null which is DBNull.Value

 command.Parameters.AddWithValue("Date", (item.Start) ?? DBNull.Value);

Upvotes: 1

Related Questions