1

I have a simple piece of code

bool retrievalAttempted = Convert.ToBoolean(reader.GetByte(1));

where column 1 is of type BIT NOT NULL and yet I keep getting the exception

System.InvalidCastException: Specified cast is not valid.

What am I doing wrong here?

1
  • 1
    The bit type corresponds to a boolean, not a byte Commented Mar 15, 2017 at 15:11

2 Answers 2

6

If it's a bit-column you have to use reader.GetBoolean:

bool retrievalAttempted = reader.GetBoolean(1);

SQL Server Data Type Mappings

bit       Boolean       Bit       GetSqlBoolean       Boolean       GetBoolean

GetByte is only used for tinyint-columns.

Sign up to request clarification or add additional context in comments.

Comments

0

A bit is different than a byte !

Look at this link for sql server types vs .net types SQL Server Data Type Mappings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.