Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If checkbox is checked then
send the bool value (1) to the table column in database
Else
send the bool value (0) to the table column in database

What I have tried:

<asp:CheckBox ID="CheckBox1" runat="server" CssClass="verdana8BG" AutoPostBack="false" Text="Dot Net framework" onclick="inputsChanged()" />  

'If CheckBox1.Checked Then
            '    theData("Course") = 1
            'Else
            '    theData("Course") = 0
            'End If


My friend is saying that it's a kind of sending hardcoded values.
Posted
Updated 3-Jun-21 20:06pm
v2

If you prefer it a little more readable than Chris suggests, then:
VB
theData("Course") = If(ChecklBox1.Checked, 1, 0)
 
Share this answer
 
Comments
Chris Copeland 27-May-21 10:52am    
Wasn't aware of the If function, learn something new every day!
OriginalGriff 27-May-21 11:39am    
IIRC, it used to be the IIf Function? Not sure when they added the If operator ... you know what they are like for changes ... :sigh:
Member 14953087 28-May-21 7:35am    
This is true
An alternative in VB.NET is to cast the boolean directly to an integer. The only drawback of this is that it will convert a True into -1 due to how the bits operate in a signed integer. However you can mitigate this by doing something like:

VB.NET
Math.Abs(CInt(CheckBox1.Checked))

However don't be afraid to create conditional blocks if it's what you think you need for readability and maintenance, some people will push for the most minimal code you can but in reality sometimes it's better to have things laid out in a way that is more readable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900