So today I was working on a project and came across the issue of extracting a tinyint value from the database using ADO.NET. I also had to ensure that the returned value wasn’t NULL or else the conversion would throw a System.InvalidCastException simply because it can’t cast NULL to an int. So the two things that I had to deal with was:
- Check for NULL values
- Cast tinyint to regular int
So, here is the implementation that I came up with. Now there may be an easier way to check for NULL values returned from database, in fact I am pretty sure there is.
But basically to convert tinyint to int you just have to extract it as a BYTE and assign it to an int value and the conversion happens automatically.




Leave a comment