Results 1 to 7 of 7

Thread: [RESOLVED] Radio buttons and SQL

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Resolved [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?

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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?

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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?

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Property CheckedRadioButton() As Integer
    2.     Get
    3.         If RadioButton1.Checked Then Return 1
    4.         If RadioButton2.Checked Then Return 2
    5.         If RadioButton3.Checked Then Return 3
    6.     End Get
    7.     Set(ByVal value As Integer)
    8.         Select Case value
    9.             Case 1 : RadioButton1.Checked = True
    10.             Case 2 : RadioButton2.Checked = True
    11.             Case 3 : RadioButton3.Checked = True
    12.         End Select
    13.     End Set
    14. End Property
    So now when you want to set it's value, you can assign it that number:
    e.g.
    vb.net Code:
    1. 'to select first radio button
    2. CheckedRadioButton = 1  
    3.  
    4. 'to select 2nd radio button
    5. CheckedRadioButton = 2  
    6.  
    7. 'to select whatever is stored in your database
    8. CheckedRadioButton = objDataTable.Rows(0).Item("MatCharacter")

    And get the selected radiobutton like this:
    vb.net Code:
    1. 'get it in myVar
    2. myVar = CheckedRadioButton
    3.  
    4. 'show the selected value in messagebox
    5. MsgBox(CheckedRadioButton)
    6.  
    7. 'save value to database
    8. objDataAdapter.InsertCommand.Parameters.AddWithValue("@MatCharacter", CheckedRadioButton)
    Last edited by Pradeep1210; Feb 26th, 2009 at 09:51 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width