[RESOLVED] Radio buttons and SQL
Hi, I have several textboxes in my form and a groubbox that have four radiobuttons. I want all the values of the textboxes (and the selection of the radiobuttons) to be stored in sql server. As far as the values of the textboxes in concerned, I managed to do it. But what about the selection of the radiobuttons??? This is the code I am using for the textboxes:
objConnection.Open()
objDataAdapter.InsertCommand = New SqlCommand()
objDataAdapter.InsertCommand.Connection = objConnection
objDataAdapter.InsertCommand.CommandText = "INSERT INTO Material " & _
"(TxtCode, TxtDescr, TxtForDescr, TxtBarcode)" & _
"VALUES (@TxtCode, @TxtDescr,@TxtForDescr, @TxtBarcode)"
objDataAdapter.InsertCommand.Parameters.AddWithValue("@TxtCode", TxtCode.Text)
objDataAdapter.InsertCommand.Parameters.AddWithValue("@TxtDescr", TxtDescr.Text)
objDataAdapter.InsertCommand.Parameters.AddWithValue("@TxtForDescr", TxtForDescr.Text)
objDataAdapter.InsertCommand.Parameters.AddWithValue("@TxtBarcode", TxtBarcode.Text)
objDataAdapter.InsertCommand.ExecuteNonQuery()
objConnection.Close()
I think that I have to write in the database numbers and not the texts of the radiobuttons. Any ideas on how can I do this?
Re: Radio buttons and SQL
Hello,
What do the RadioButton choices represent?
It may be a good idea to create an enumeration which matches the choices of the radio buttons and write this enumeration value to the database, and then cast that choice back to the enumeration value.
Gary
Re: Radio buttons and SQL
Thank you for answering. Can you give more details or an example because I didn't understand it well. Where do you mean I have to create this enumeration...as a field in the database table?
Re: Radio buttons and SQL
Hey,
No, the enumeration would be in your code, and it would represent the choices of the radio buttons.
Have a look here for details of using the enumeration:
http://www.startvbdotnet.com/language/enumeration.aspx
and
http://aspalliance.com/292
The enumeration simply represents the choices that the user can make. The text of the enumeration could be used to populate the radio button texts, and when you are saving to the database you save the numerical equivalent of the enumeration.
Hope that makes sense.
Gary
Re: Radio buttons and SQL
Now I understood what enumeration is. I did this matching:
Enum Item
Simple= 1
Set = 2
Compose = 3
End Enum
But how can write the code in the sql query because in the link http://aspalliance.com/292 it doesn't have to do with database. I tried something like:
objDataAdapter.InsertCommand.CommandText = "INSERT INTO Material " & _
"(TxtCode, TxtDescr, TxtForDescr, TxtBarcode, TxtAlterCode, MatCharacter) & _
"VALUES (@TxtCode, @TxtDescr, @TxtForDescr, @TxtBarcode, @SetRadiobuttons.Selected.Value)"
objDataAdapter.InsertCommand.Parameters.AddWithValue("@SetRadiobuttons.Selected.Value", MatCharacter.Text)
MatCharacter is the name of the field in the database table and SetRadiobuttons is the AccessibleName property for the radiobuttons.
Of course it doesn't work. Do you have any ideas for solving it?
Re: Radio buttons and SQL
Here is one simple and easy way (though might not be the best; some people here might even frown upon me how I'm using the property ;) ):
1. Add the following property to your form. I assume the radiobuttons are named RadioButton1, RadioButton2, RadioButton3.
vb.net Code:
Private Property CheckedRadioButton() As Integer
Get
If RadioButton1.Checked Then Return 1
If RadioButton2.Checked Then Return 2
If RadioButton3.Checked Then Return 3
End Get
Set(ByVal value As Integer)
Select Case value
Case 1 : RadioButton1.Checked = True
Case 2 : RadioButton2.Checked = True
Case 3 : RadioButton3.Checked = True
End Select
End Set
End Property
So now when you want to set it's value, you can assign it that number:
e.g.
vb.net Code:
'to select first radio button
CheckedRadioButton = 1
'to select 2nd radio button
CheckedRadioButton = 2
'to select whatever is stored in your database
CheckedRadioButton = objDataTable.Rows(0).Item("MatCharacter")
And get the selected radiobutton like this:
vb.net Code:
'get it in myVar
myVar = CheckedRadioButton
'show the selected value in messagebox
MsgBox(CheckedRadioButton)
'save value to database
objDataAdapter.InsertCommand.Parameters.AddWithValue("@MatCharacter", CheckedRadioButton)
Re: Radio buttons and SQL
Thank you it works now really fine. I suppose that the other person was also tried to tell me something similar but I couldn't understand it very well. Thank both of you