Insert RadioButton Value

JohnDW

Well-known member
Joined
Jun 13, 2012
Messages
60
Location
Antwerp, Belgium
Programming Experience
1-3
I have three RadioButtons in a form which the user must choose. With choice comes a value ranging from 1 to 3 in a text field, and I use that value in the insert query to a table in sql server update.
This does not work, because the value in the field of sql server is always 1.
How this coming?
Additionally I want the user require 1 of the Radio Buttons to use.
I have not found how I can achieve.
Thanks,
John

VB.NET:
Expand Collapse Copy
Dim SQL As String = "insert into Orderscope (Klantnummer, Orderdatum, IsBetaald ) values (@Klantnummer, @Orderdatum, @IsBetaald)"
Using connection As New SqlConnection(Cnst)
Dim CMD1 As New SqlCommand(SQL, MyConnection)
CMD1.Parameters.AddWithValue("@Klantnummer", CInt(txtKlantnummer.Text))
CMD1.Parameters.AddWithValue("@Orderdatum", CDate(TxtOrderdatum.Text))
CMD1.Parameters.AddWithValue("@IsBetaald", CByte(IsBetaald.Text))
End Using



IsBetaald.Text = CStr(1)
EndSub
PrivateSub Rb2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rb2.CheckedChanged
IsBetaald.Text = CStr(2)
EndSub
PrivateSub Rb3CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rb3.CheckedChanged
IsBetaald.Text = CStr(3)
EndSub
 
I wouldn't even bother handling the CheckChanged event. Just get the value when you need it.
Dim radioButtons = New RadioButton() {RadioButton1, RadioButton2, RadioButton3}
Dim radioButtonValue = Array.IndexOf(radioButtons, radioButtons.Single(Function(rb) rb.Checked))
That gets the single RadioButton that's checked from the array and then gets its index in the array. That will give you a value from 0 to 2, so you can add 1 to it if that's what you need.
 
OK, I'm trying but In which event do I code?

And when I type
VB.NET:
Expand Collapse Copy
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] radioButtons = [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]RadioButton[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000]() {Rb1, Rb2, Rb3}[/COLOR][/SIZE][/FONT][/SIZE][/FONT]

Intellisense says : option strict on requires all variables declarations to have an 'As' clause

VB.NET:
Expand Collapse Copy
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] Dim radioButtonValue = [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Array[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000].IndexOf(radioButtons, radioButtons.Single([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000](rb) rb.Checked))[/COLOR][/SIZE][/FONT][/SIZE][/FONT]

intellisense says option strict on disallows late binding.

Can you tell me what this means?

John

 
Last edited:
JohnDW said:
OK, I'm trying but In which event do I code?
jmcilhinney said:
Just get the value when you need it.
When do you need the value? That tells you where to put the code.

As for the rest, have you turned Option Infer Off in the project properties? With Option Infer On, which it is by default for .NET 3.5 projects, the code you have should work as is.
 
It works. The insert places the index in the column,
but the index is not displayed correctly.
For index 0, 0 is displayed
for index 1 and 2 is only 1 displayed.

Question: where can I change the index (range 1-3 instead of 0-2)?

Thanks,

John
 
Don't assume that just because you're new to programming that you can't do anything yourself. The answer to your question has nothing really to do with programming. If you had a pen and paper and I gave you numbers in the range 0 to 2 and I told you to map them to numbers in the range 1 to 3, what would you do? Why would this be any different? Common sense and logic don't have any less of a place in programming than anywhere else. As a clue, I've already answered this question in post #2.
 
Back
Top