-4
static public string CreatePlaylist(int userId, string playlistName, int playlistImgId)
{
    DataTable playlistTable = Dal.CreatePlaylist(userId, playlistName, playlistImgId);

    Playlist p = new Playlist((int)playlistTable.Rows[0]["UserID"]);

    p.PlaylistName = playlistTable.Rows[0]["Playlist_Name"] == DBNull.Value ? null : (string)playlistTable.Rows[0]["Playlist_Name"];
    p.PlaylistImg = playlistTable.Rows[0]["Playlist_Img_ID"] == DBNull.Value ? null :  (int)playlistTable.Rows[0]["Playlist_img_ID"];

    return new JavaScriptSerializer().Serialize(p);

}

I get this error :

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

I found similar questions as this but didn't understand their solution.

ExtraSun
  • 528
  • 2
  • 11
  • 31

1 Answers1

3

You are casting playlistTable.Rows[0]["Playlist_img_ID"] to int, but you have a ternary operation (?:) where you have null as the other operand.

int and null are not "compatible" for the ternary operation. You should use the nullable int? type.

Instead of:

p.PlaylistImg = playlistTable.Rows[0]["Playlist_Img_ID"] == DBNull.Value ? null :  (int)playlistTable.Rows[0]["Playlist_img_ID"];

Do:

p.PlaylistImg = playlistTable.Rows[0]["Playlist_Img_ID"] == DBNull.Value ? null :  (int?)playlistTable.Rows[0]["Playlist_img_ID"];

See Conditional operator assignment with Nullable<value> types?

N Sarj
  • 374
  • 3
  • 11
  • 2
    Since our answers are basically the same, I've deleted mine and upvoted yours. You seen to need the reputation points more than me. – Zohar Peled Dec 24 '18 at 13:50
  • And what if `PlaylistImg` is declared as `int`? This just provokes another compilation error. It is best to wait until we have enough information and not guess – Camilo Terevinto Dec 24 '18 at 13:50
  • @Camilo, you are right. Your comment is more accurate. – N Sarj Dec 24 '18 at 13:52