3
advert.AC = String.IsNullOrEmpty(reader["AC"].ToString()) ? null : Byte.Parse(reader["AC"].ToString());

I want to assign null to the property named AC when there is null record in reader["AC"] otherwise assign the value to AC by parsing it into Byte. The type of AC is "Byte?" in my case but it is giving an error in the above assignment.

Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'byte' C:\Users\Waheed Ahmed\documents\visual studio 2010\Projects\Autos\Autos\Controllers\autosController.cs 274 85 Autos

Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64

1 Answers1

8

You can refer here Conditional operator assignment with Nullable<value> types?

If you do need to cast the null to Byte? and then use it , like

advert.AC = String.IsNullOrEmpty(reader["AC"].ToString()) ? (Byte?)null : Byte.Parse(reader["AC"].ToString());
Community
  • 1
  • 1
achakravarty
  • 418
  • 3
  • 7