save bit as 0 or 1not true or false

  • Thread starter Thread starter GotDotNet?
  • Start date Start date
G

GotDotNet?

I have a bit field in my dataset that reads true or false, but i need to
save it to the db bit column as 0 or 1, how can i convert the true or false
to 0 or 1 so it saves into the table?
 
GotDotNet? said:
I have a bit field in my dataset that reads true or false, but i need to
save it to the db bit column as 0 or 1, how can i convert the true or false
to 0 or 1 so it saves into the table?

bool bit = true; // (or false)

int zeroOrOne = (bit) ? 1 : 0;

Save zeroOrOne to the db.

HTH,
Sven
 
Hi,

i think you do not need to write a type cast for your case. if you use a
value of type boolean in your code and your db field is a bit the cast will
be done automaticly. BUT if your db field is set to nullable you have to
check DBNull first when you read the value. Or you declare your type on code
side as nullable (for example: "private boolean? myValue").

HTH
 
You shouldn't have to do anything. Just because the representation in
..NET is true and false, the provider (which I assume are the classes in the
System.Data.SqlClient namespace) will translate that appropriately to 0 and
1 on the database side (assuming you are using parameterize queries or
stored procedures).

Hope this helps.
 

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

Back
Top