0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rico Strydom
  • 537
  • 1
  • 6
  • 26
  • You don't want to pass null to your database anyway, you want to pass `System.DBNull.Value` – Dale K Oct 11 '22 at 18:53
  • 1
    You probably have to cast everything to an object, since C# doesn't handle null guids but SQL Server does e.g. `InputID == null ? (object)System.DBNull.Value : (object)Guid.Parse(InputFromTestCaseID.ToString())` – Dale K Oct 11 '22 at 19:48
  • @DaleK, That did the trick! Thanks a million. – Rico Strydom Oct 12 '22 at 08:04

1 Answers1

2

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.

riffnl
  • 3,248
  • 1
  • 19
  • 32