simple question about casting from DB

D

Dariusz Tomon

Hi

I'm trying to retrieve data from database (MS Sql serv) from field of BIT
type.
I'm trying like this:
while (myReader.Read())

{

Int16 cos = (Int16)myReader.GetSqlInt16(1);

......

I build the solution and when I'm trying to open app. I get:

Specified cast is not valid.
How can I retrieve sql BIT type into C#. Maybe there is some general
advice/tutorial of types casting form DB to c# on the net?

Thanks for help in advance



Darek
 
N

Nicholas Paldino [.NET/C# MVP]

Dariusz,

You don't need the cast, because the GetSqlInt16 method should return an
Int16 type. However, I believe the issue is that you need to call the
GetBoolean method, since the bit field translates to a boolean value.

Hope this helps.
 
D

Dariusz Tomon

I followed your advice:

bool cos = (bool)myReader.GetSqlBoolean(1);

and it works - thank you very much

Darek


Nicholas Paldino said:
Dariusz,

You don't need the cast, because the GetSqlInt16 method should return
an Int16 type. However, I believe the issue is that you need to call the
GetBoolean method, since the bit field translates to a boolean value.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dariusz Tomon said:
Hi

I'm trying to retrieve data from database (MS Sql serv) from field of BIT
type.
I'm trying like this:
while (myReader.Read())

{

Int16 cos = (Int16)myReader.GetSqlInt16(1);

.....

I build the solution and when I'm trying to open app. I get:

Specified cast is not valid.
How can I retrieve sql BIT type into C#. Maybe there is some general
advice/tutorial of types casting form DB to c# on the net?

Thanks for help in advance



Darek
 
N

Nicholas Paldino [.NET/C# MVP]

Dariusz,

Note, there is no need for the cast to bool. You should be able to call
GetBoolean, and have it return a boolean value.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dariusz Tomon said:
I followed your advice:

bool cos = (bool)myReader.GetSqlBoolean(1);

and it works - thank you very much

Darek


Nicholas Paldino said:
Dariusz,

You don't need the cast, because the GetSqlInt16 method should return
an Int16 type. However, I believe the issue is that you need to call the
GetBoolean method, since the bit field translates to a boolean value.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dariusz Tomon said:
Hi

I'm trying to retrieve data from database (MS Sql serv) from field of
BIT type.
I'm trying like this:
while (myReader.Read())

{

Int16 cos = (Int16)myReader.GetSqlInt16(1);

.....

I build the solution and when I'm trying to open app. I get:

Specified cast is not valid.
How can I retrieve sql BIT type into C#. Maybe there is some general
advice/tutorial of types casting form DB to c# on the net?

Thanks for help in advance



Darek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top