Help with using a radio button in ado.net database

NubiBlue

New member
Joined
Dec 21, 2010
Messages
3
Programming Experience
3-5
Hi folks,

I am starting to relearn visual basic.net from its previous incarnation of visual basic 6. I'm started to use ado.net and while I am OK with the text box, and even reading the check box, I can't find the code I need to be able to let the database know which radio button the user has selected.

Components as follows;

MS Access database with a field with three options to choose from, North, Midlands or South (these are just test terms).

On my form I have three corresponding buttons rdoNorth, rdoMidlands, rdoSouth.

The code after opening access is as follows;

txtFirstName.Text = ds.Tables("clientList").Rows(inc).Item(1)
txtSurname.Text = ds.Tables("ClientList").Rows(inc).Item(2)
txtAddress1.Text = ds.Tables("ClientList").Rows(inc).Item(3)
txtAddress2.Text = ds.Tables("ClientList").Rows(inc).Item(4)
txtAddress3.Text = ds.Tables("ClientList").Rows(inc).Item(5)
txtPostcode.Text = ds.Tables("ClientList").Rows(inc).Item(6)
chkMember.Checked = ds.Tables("ClientList").Rows(inc).Item(8)

I'm at a total lost as to how to write the code for the radio button.

I hope that I've explained myself. Can anyone assist me or point me to a useful resource?

P.S. If I've posted in the wrong forum, apologise in advance. Thank you.
 
There's no such thing as an ADO.NET database. ADO.NET is a technology for reading data from and writing data to a data source. That data source could be just about any database and many non-database data sources too.

Your question really has nothing to do with ADO.NET. In your case, you're using a DataRow and that DataRow will have one field that corresponds to the RadioButtons. That field will most likely contain text or a number. That's it for ADO.NET. You get the text or number in exactly the same way as you would any other time and you set the text or number the same as you would any other time. Your question is pure and simple:

1. How to decide which RadioButton to check based on the value of some text or a number.
2. How to get some text or a number based on which RadioButton is checked.

The answer is that you use the same conditional logic you always do when you want to decide between or amongst options, i.e. Select Case or If
VB.NET:
Select Case CStr(myDataRow("Region"))
    Case "North"
        Me.rdoNorth.Checked = True
    Case "Midlands"
        Me.rdoMidlands.Checked = True
    Case "South"
        Me.rdoSouth.Checked = True
End Select
VB.NET:
If Me.rdoNorth.Checked Then
    myDataRow("Region") = "North"
ElseIf Me.rdoMidlands.Checked Then
    myDataRow("Midlands") = "North"
ElseIf Me.rdoSouth.Checked Then
    myDataRow("Region") = "South"
End If
It's important to break problems down so that you know what you're actually dealing with. By think of this as an ADO.NET problem, you were actually distracting yourself from the actual issue.
 
Thank you for your response to my request. I accept that I used an incorrect term in relation to ADO.NET. I also except that my question was a little unfair and vague in that its presentation did not provide you with enough information to provide me with an answer. I have sought information elsewhere and I present the answer below in the hope that it might help others grappling with the same question.


Dim radString As String = ds.Tables("ClientList").Rows(inc).Item(9)

If radString = "North" Then
rdoNorth.Checked = True
ElseIf radString = "Midland" Then
rdoMidlands.Checked = True
ElseIf radString = "South" Then
rdoSouth.Checked = True
End If

I won't post the full code from dim to connection onwards, but I hope that this helps someone.

Seasons greetings to one and all.
 
I have sought information elsewhere and I present the answer below in the hope that it might help others grappling with the same question.

VB.NET:
        Dim radString As String = ds.Tables("ClientList").Rows(inc).Item(9)

        If radString = "North" Then
            rdoNorth.Checked = True
        ElseIf radString = "Midland" Then
            rdoMidlands.Checked = True
        ElseIf radString = "South" Then
            rdoSouth.Checked = True
        End If
You have presented "an" answer, but I would suggest that the answer I presented above is the better choice. In the situation where you want to test one value against many, Select Case is considered preferable to If...ElseIf. Modifying my first code snippet to be more specific to your project:
VB.NET:
Select Case CStr(ds.Tables("ClientList").Rows(inc)(9))
    Case "North"
        Me.rdoNorth.Checked = True
    Case "Midlands"
        Me.rdoMidlands.Checked = True
    Case "South"
        Me.rdoSouth.Checked = True
End Select
 
Thanks for your help, and sorry once again for not providing enough relevant information in th first place. As you note both work, but my preference is the "if...then" structure as I use it more often and for me, its easier to remember. Thanks for the modified code, I hope that both also help other people which was one of my purposes for posting.
 
Back
Top