Friday, 15 April 2011

c# - What value does " sqlDataReader["dbIntegerField"] as String " yield? -



c# - What value does " sqlDataReader["dbIntegerField"] as String " yield? -

old code spelunking here.

the field i'm reading integer field in database (sql server)

it read in, , unknown reason, assigned string property in session object such:

sessionobject.category = reader["category"] string;

the actual values bitfield, 0 - 15;

the sessionobject stored in session, retrieved later, , info appears present.

at stage, category fellow member converted int32 such:

int categories = convert.toint32( sessionobject.category );

however, tests:

w.write(((categories & 1) == 1)? cehours :0); ... w.write(((categories & 2) == 2)? cehours :0); ... w.write(((categories & 4) == 4)? cehours :0); ... w.write(((categories & 8) == 8)? cehours :0);

all print 0.

moron check:

database values correct, status should true, cehours not 0, query tested fully, etc, etc, etc,

without other changes, changed sessionobject field int, converted int32 on reading, , dropped string int conversion in later code. worked fine.

so wrong first way? (other beingness awkward.)

the integer field arrive in info reader boxed int32 value. there 2 ways of getting value out, either unboxing using (int)reader["category"] or running tostring method on value using reader["category"].tostring(). latter naturaly returns string rather integer.

when seek value casting string, null reference back. using as operator designed , silently fail , homecoming null instead of throwing exception because casting not possible.

when later utilize convert.toint32 on null reference, value 0. method designed homecoming null reference.

c# sql

No comments:

Post a Comment