|
Subject:
|
Save Checkbox with boolean 0/1 value
|
|
Posted By:
|
rdove84
|
Post Date:
|
10/23/2006 3:34:38 PM
|
I have several checkboxes on a web form whose values I need to save to a database, then be able to retrieve them to an editable form. I know that to retrieve them, I need the check box to save not as the usual true/false answer but as a tinyint 1 or 0 in a SQL database table.
Is there C# code available to help me figure this out?
rdove84
|
|
Reply By:
|
dparsons
|
Reply Date:
|
10/23/2006 4:23:37 PM
|
Just create a variable
int i; if(checkbox.checked){ i = 1; } else{ i = 0; }
------------------------- I will only tell you how to do it, not do it for you. Unless, of course, you want to hire me to do work for you.
^^Thats my signature
|
|
Reply By:
|
rdove84
|
Reply Date:
|
10/24/2006 8:45:55 AM
|
Thank you for such a quick reply.
I have one more question. I'm saving my information via a stored procedure into a column marked tinyint. All of my other columns from my form are saved via lines like this:
f1.ELECTRICITY = this.electricity.SelectedValue; (or Text, etc)
However, when I try to save the Checkbox items I get one of two messages. The item cannot be implicitly converted from string to int or cannot implicitly convert bool. I thought at first after the making my checkbox equal 1 or 0 that I then should call a line like the one above (f1.HUMANSUBJECT = this.humanSubject.Checked) but I keep getting errors. Is there something else I should be doing?
rdove84
|
|
Reply By:
|
dparsons
|
Reply Date:
|
10/24/2006 8:54:21 AM
|
ok you can't do f1.HUMANSUBJECT = this.humanSubject.Checked if you have declared f1.HUMANSUBJECT as an integer because the return type of this.humanSubject.Checked is a boolean. I see 2 choices:
1)Change your database datatable column from tinyint to a BIT datatype and you can pass in a bool value (TRUE/FALSE) 2)You will have to use an If statement to determine what the value of your int will be.
hth
------------------------- I will only tell you how to do it, not do it for you. Unless, of course, you want to hire me to do work for you.
^^Thats my signature
|
|
Reply By:
|
rdove84
|
Reply Date:
|
10/24/2006 9:11:18 AM
|
Thank you so much that did the trick.
|
|
Reply By:
|
dparsons
|
Reply Date:
|
10/24/2006 9:12:39 AM
|
No problem glad it worked. =]
------------------------- I will only tell you how to do it, not do it for you. Unless, of course, you want to hire me to do work for you.
^^Thats my signature
|