Update or insert null value in uniqueidentifier column

524 views Asked by At

I have a SQL Server database with a table that has a uniqueidentifier column InputID that allows null values.

In my C# project, I am passing the value of InputID as object InputID to my SqlDataAdapter and there I am parsing the object as Guid if it is not null.

sqlDataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@InputID", SqlDbType.UniqueIdentifier)).Value = InputID == null ? null : Guid.Parse(InputFromTestCaseID.ToString());

Unfortunately the

InputID == null ? null : Guid.Parse(InputFromTestCaseID.ToString());

throws an error

CS8370 Feature 'target-typed conditional expression' is not available in C# 7.3. Please use language version 9.0 or greater.

What am I doing wrong?

1

There are 1 answers

0
riffnl On

The error message should read: "null is not a Guid, so I have no idea what you want me to do"

I'm not exactly sure if this will work (didn't try)

sqlDataAdapter.UpdateCommand.Parameters.Add(new SqlParameter("@InputID", SqlDbType.UniqueIdentifier)).Value = (Guid.TryParse(InputFromTestCaseID.ToString(), out var value) ? value : null);

Otherwise, first create a nullable Guid as a value-container.