-1

I am trying to read my database. I have an column stores int values or null values. When I try to read this column i normally use this line of code;

int Score = Convert.ToInt32(reader["Score"]);

but it can be null. So i cant convert it to int. Then I tried ;

bool tryGetScore = int.TryParse(reader["Score"], out player1Score);

but i cant parse this value neither. It says "Cannot convert from 'Object' to 'System.ReadOnlySpan'"

How can i read this value. Whenever i face a null, my program crashes

Shadow
  • 33,525
  • 10
  • 51
  • 64
Tevres
  • 11
  • Does this answer your question? [casting datareader value to a to a Nullable variable](https://stackoverflow.com/questions/5409936/casting-datareader-value-to-a-to-a-nullable-variable) – GSerg Jun 05 '22 at 15:12

1 Answers1

0

You can try use isDbNull for example

int scoreIdx = reader.GetOrdinal("Score");
int score = reader.IsDbNull(scoreIdx) ? 
  0 : reader.GetInt32(scoreIdx);
Alexey Zelenin
  • 710
  • 4
  • 10