468,770 Members | 2,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,770 developers. It's quick & easy.

CheckBox.checked return a value

All,

Have a problem i just cant find the answer for.
I'm trying to populate a colum in a SQL database. Basically the
column requires a 1 or 0.
I'm trying to populate this col via a c# checkbox.

so basically:
checkbox.checked =1
else
=0

any ideas how to get this working? ive set the variable as a decimal
(is that correct?)

Thanks
Jan 31 '08 #1
3 6946
Hi,
so basically:
checkbox.checked =1
else
=0

any ideas how to get this working? ive set the variable as a decimal
(is that correct?)
Hope this is an answer to your question, try this:

int valueInSql = 1;
checkbox.Checked = (valueInSql == 1 ? true : false)

or even simply

checkbox.Checked = (valueInSql == 1);

Note that this doesn't alarm you if the value in sql was anything
other than 0 or 1.

PS. As in the example, I'd use an int for this instead of decimal, or
at least any other whole-number type, one that is large enough to hold
the type of the column in your DB.

Regards,
Jeroen
Jan 31 '08 #2
On 31 Jan, 10:36, Jeroen <mercu...@gmail.comwrote:
Hi,
so basically:
checkbox.checked =1
else
=0
any ideas how to get this working? ive set the variable as a decimal
(is that correct?)

Hope this is an answer to your question, try this:

int valueInSql = 1;
checkbox.Checked = (valueInSql == 1 ? true : false)

or even simply

checkbox.Checked = (valueInSql == 1);

Note that this doesn't alarm you if the value in sql was anything
other than 0 or 1.

PS. As in the example, I'd use an int for this instead of decimal, or
at least any other whole-number type, one that is large enough to hold
the type of the column in your DB.

Regards,
Jeroen
Thanks you mate, works a treat!
Jan 31 '08 #3
If you are binding the column to some other object which is typed as an
integer, then you might want to attach to the Format and Parse methods on
the Binding to transform the value between the data source and the bound
control. This will let you convert from an integer to a boolean and vice
versa.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"nologo" <ze****@hotmail.comwrote in message
news:ed**********************************@i72g2000 hsd.googlegroups.com...
All,

Have a problem i just cant find the answer for.
I'm trying to populate a colum in a SQL database. Basically the
column requires a 1 or 0.
I'm trying to populate this col via a c# checkbox.

so basically:
checkbox.checked =1
else
=0

any ideas how to get this working? ive set the variable as a decimal
(is that correct?)

Thanks
Jan 31 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steph | last post: by
6 posts views Thread by Dennis Allen | last post: by
reply views Thread by Zürcher See | last post: by
6 posts views Thread by Alessandro Rossi | last post: by
34 posts views Thread by clinttoris | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.