Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

CheckBox.checked return a value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nologo - 31 Jan 2008 10:24 GMT
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
Jeroen - 31 Jan 2008 10:36 GMT
Hi,

> so basically:
> checkbox.checked =1
[quoted text clipped - 3 lines]
> 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
nologo - 31 Jan 2008 11:10 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> Regards,
> Jeroen

Thanks you mate, works a treat!
nologo - 31 Jan 2008 14:44 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> 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?
Nicholas Paldino [.NET/C# MVP] - 31 Jan 2008 13:10 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> All,
>
[quoted text clipped - 12 lines]
>
> Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.