CheckBoxes
CheckBoxes
CheckBoxes
CheckBoxes
CheckBoxes
CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes
CheckBoxes CheckBoxes
CheckBoxes
Go Back  Xtreme Visual Basic Talk > > > CheckBoxes


Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2015, 05:29 AM
dr223 dr223 is offline
Centurion
 
Join Date: Feb 2009
Posts: 114
Default CheckBoxes


Hi,

I have on my form the following question;

Q2. Do you consider yourself to be in good health? CheckBox Yes CheckBox No CheckBox I don't Know

CheckBox Yes = chkQ2Y
CheckBox No = chkQ2N
CheckBox I don't Know = chkQ2DntKnw


Now, I have;

Code:
			Using sqlConn As SqlConnection = GetDbConnection()
				Using sqlCmnd As New SqlCommand("INSERT INTO dbo.TblPatInfo (DateOfQuestionnaire, Health) Values (@DateOfQuestionnaire, @Health)", sqlConn)
					With sqlCmnd.Parameters
						.AddWithValue("@DateOfQuestionnaire", IIf(txtDateOfQues.Text.Length > 0, txtDateOfQues.Text, DBNull.Value))
						.AddWithValue("@Health", IIf(chkQ2Y.Checked = 1, chkQ2N.Checked = 2,  chkQ2DntKnw
.Checked = 3, DBNull.Value))

					End With
					sqlCmnd.ExecuteNonQuery()
					sqlConn.Close()
				End Using
			End Using
The issue is the Q2 field (Health) is always NULL. Even if I checked any of the checkboxes, it doesn't enter to the database the reflected value.
Where am I going wrong?

N/B: I have set standards procedures that ONLY one checkbox could be checked at any one time.


Thanks
Reply With Quote
  #2  
Old 02-09-2015, 06:38 AM
PlausiblyDamp's Avatar
PlausiblyDampCheckBoxes PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Not entirely sure what the IIf statement is supposed to be doing there, unless I have missed something IIf only takes three parameters and not 4.

The .Checked parameter is a boolean and not an integer so comparing to 1, 2 or 3 makes no sense (Try Option Strict On and see if it throws any errors)

Also why not use a radio button if only a single option can be selected at a time? Radio buttons are designed to work this way and users expect them to work that way, users however expect check boxes to allow multiple selections.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 02-09-2015, 06:45 AM
dr223 dr223 is offline
Centurion
 
Join Date: Feb 2009
Posts: 114
Default

Ok Changed them to Radio Buttons.

Now I have;

RadioButton Yes = RDQ2Y
RadioButton No = RDkQ2N
RadioButton I don't Know = RDQ2DntKnw


How can I update the .ADDParamets to enter the selected radiobutton

Thanks
Reply With Quote
  #4  
Old 02-09-2015, 07:53 AM
PlausiblyDamp's Avatar
PlausiblyDampCheckBoxes PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Should the parameter just be equal to 1, 2 or 3 or is null also allowed?

You could always just use another variable and set it to 1, 2 or 3 depending on the state of the radio buttons and use that for the parameter's value.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 02-09-2015, 08:50 AM
dr223 dr223 is offline
Centurion
 
Join Date: Feb 2009
Posts: 114
Default

Null is also allowed incase No radio button is selected..

Please help me with

.AddParameter code

Thanks alot
Reply With Quote
  #6  
Old 02-09-2015, 04:24 PM
PlausiblyDamp's Avatar
PlausiblyDampCheckBoxes PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Out of curiosity why would you need a null? Surely one of the 3 options would be relevant or possibly a 4th (declined to answer?)

However you could do something like
Code:
Dim answer As Integer? = Nothing

If chkQ2Y.Checked Then
    answer = 1
ElseIf chkQ2N.Checked Then
    answer = 2
ElseIf chkQ2DntKnw.Checked Then
    answer = 3
End If
and then use the variable answer for the parameter.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 02-10-2015, 09:59 AM
dr223 dr223 is offline
Centurion
 
Join Date: Feb 2009
Posts: 114
Default

Ok - Changed the checkboxes to radio buttons and I have;

Code:
Dim Q2Selected As Integer = Nothing
		If rbQ2Y.Checked Then
			Q2Selected = 1
		ElseIf rbQ2N.Checked Then
			Q2Selected = 2
		End If
When neither of the radio buttons is checked it saves a zero (0) instead of NULL in the database.

The data type for the field is int and accepts NULL

Any help please
Reply With Quote
  #8  
Old 02-10-2015, 10:51 AM
PlausiblyDamp's Avatar
PlausiblyDampCheckBoxes PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Did you try the code I posted just above your post? Does that work?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
CheckBoxes
CheckBoxes
CheckBoxes CheckBoxes
CheckBoxes
CheckBoxes
CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes CheckBoxes
CheckBoxes
CheckBoxes
 
CheckBoxes
CheckBoxes
 
-->