-1

I want to insert a record in a database with two groupbox. groupbox1 (has Male and female radio buttons) and groupbox2 ( has New students and old students radio buttons)

enter image description here

In this case there will be four possible input the needs to be check

  1. Male and old students
  2. Male and new students
  3. Female and old students
  4. Female and New students

Normally I will check each radio button individually just like this

   If rbtMale.Checked And rbtOld.Checked Then
            Dim OldStudent = rbtOld.Text.ToString
            Dim Male = rbtMale.Text.ToString
            Using cmd As New SqlClient.SqlCommand("dbo.uspInsert", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add(New SqlParameter("@StudPic", SqlDbType.Image))
                If (Not String.IsNullOrEmpty(Me.Name) AndAlso System.IO.File.Exists(a.FileName)) Then
                    cmd.Parameters("@StudPic").Value = System.IO.File.ReadAllBytes(a.FileName)
                    cmd.Parameters.Add("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
                    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
                    cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Male
                    cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = OldStudent
                    cmd.ExecuteNonQuery()
                    MsgBox("Save Record New record Successfully")
               End If
            End Using
        End If

But I think there is smart way to check which radio button being checked in a groupbox and insert it in a database. Below are some codes that gives me idea about this but the problem is that i do not know how to pass the value in a groupbox then insert this in my db. Any help would be very much appreciated. Thanks

For Each Ctrl In GroupBox1.Controls
    If Ctrl.checked Then MsgBox(Ctrl.name)
Next
Diether Silverious
  • 199
  • 1
  • 5
  • 23

1 Answers1

4

You're nearly there in the snippet at the bottom of your question. This function takes a groupbox as the parameter and returns the RadioButton that is checked

Private Function GetGroupBoxCheckedButton(grpb As GroupBox) as RadioButton
    For Each ctrl As RadioButton In grpb.Controls
        If ctrl.Checked Then Return ctrl
    Next
End Function

You'll get a compiler warning about the function not returning a result on all code paths, but this shouldn't matter with RadioButtons - assuming that you have more than one radiobutton in the groupbox - in theory.

Alternatively you could use this instead. It wont generate a compiler warning, but is less readable:-

Private Function GetGroupBoxCheckedButton(grpb As GroupBox) As RadioButton
    Dim rButton As RadioButton = grpb.Controls.OfType(Of RadioButton).Where(Function(r) r.Checked = True).FirstOrDefault()
    Return rButton
End Function

To get the text value just use something like

Dim value As String  = GetGroupBoxCheckedButton(GroupBox1).Text 

I'm guessing that all you want to do here is to add the RadioButton text to your database and rather than write the above code multiple times, you just want to do it the once. All you'll need to is remove the If and End If statements and ammend the first bit of your code to read ..

        Dim Status As String= GetGroupBoxCheckedButton(GroupBox1).Text
        Dim Gender As String= GetGroupBoxCheckedButton(GroupBox2).Text

Incidentally, you don't need .ToString at the end of these lines as the .Text Property returns a string and no conversion to a string is needed

And finally change variables further down your code to match.

        cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Gender
        cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = Status
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Thanks for the comment. but how can i apply that in my code? how to pass the value of the radio button that is checked and insert it to my database? – Diether Silverious Apr 04 '16 at 01:59
  • The name of the radiobutton name or the text value? – David Wilson Apr 04 '16 at 02:12
  • If you want the text value, see my updated answer. You can then pass the variable into your database however you want to. – David Wilson Apr 04 '16 at 02:16
  • [Error](http://img7.uploadhouse.com/fileuploads/22303/22303137c05bc7dafc67d79ddde5eb07957c6911.png) i have this error now. Please help me to fix this – Diether Silverious Apr 04 '16 at 02:33
  • Certainly, but it is 3:40 am here and I'm falling asleep. What is the error and on which line? I'll have a look after some sleep. – David Wilson Apr 04 '16 at 02:42
  • I have a screenshot on my comment above. Please have a look after your sleep. Goodnight – Diether Silverious Apr 04 '16 at 02:52
  • I have this error with your given codes `Warning 1 Function 'GetGroupBoxCheckedButton' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.` – Diether Silverious Apr 04 '16 at 09:51
  • Ah! Sorry I didn't see the link - I was replying on my phone (and sleepy) :) The error is just a warning and the code will still run just fine as long as at least one RadioButton in the GroupBox is checked. However I'm updating my answer with a bit of Linq code which wont show the warning you can see, but still needs at least one RadioButton in the GroupBox to be checked – David Wilson Apr 04 '16 at 10:15
  • Your code is really amazing. It saves me a lot of time. Can this logic can also be applied on `checkbox` with multiple Check and insert it also to db? I hope you can give me an idea on how to deal with it if you don't mind. Thank you so much again. :-) – Diether Silverious Apr 04 '16 at 11:40
  • Thanks. I'm not sure about checkboxes. I think that this is better solved in another question rather than have a long list of comments that other user probably won't read. If you have time of course. – David Wilson Apr 04 '16 at 13:06
  • @DavidWilson WoW You went a few extra miles to provide a complete answer to this question I used the code and it works great – Vector Oct 15 '20 at 20:28