CheckBox.checked return a value

  • Thread starter Thread starter nologo
  • Start date Start date
N

nologo

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
 
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
 
Hi,



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!
 
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.
 
Hi,



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

Ok so im using:
Button btn_UpdateDatabase on click
Struct.valueinSql = 0;
chk_Box.Checked=(Struct.valueinSql==1);

This successfully updates the database once i click UPDATE..but only
with the statement: Struct.valueinSql = 0;
the following statement has no effect.
(chk_Box.Checked=(Struct.valueinSql==1);)
If i remove: Struct.valueinSql = 0;
I get the following errors:
"Use of possibly unassigned field 'valueinSql"

If i use this code:
int valueinsql = 1;
chk_Box.Checked=(Struct.valueinSql==1);

Error message: "The variable 'valueinsql' is assigned but its value is
never used"

any ideas?
 

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