Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To iterate through the values of combo box control using vb.net

I update my question here .. Am using a combo box with no of phone numbers .I want to get the phone no one by one in a variable. Now am using the below code to get the combobox values. But still now am getting the following error message System.Data.DataRowView. Please help me to fix this error. am new for vb.net.

My partial code is here ..

        For i = 0 To ComboBox1.Items.Count
            Dim s As String

            s = Convert.ToString(ComboBox1.Items(i))
        Next i
like image 273
Amulraj Avatar asked Jun 12 '12 09:06

Amulraj


People also ask

What is combo box control in VB net?

The ComboBox control is used to display a drop-down list of various items. It is a combination of a text box in which the user enters an item and a drop-down list from which the user selects an item. Let's create a combo box by dragging a ComboBox control from the Toolbox and dropping it on the form.

How do you use combo boxes in Visual Basic?

Double click the icon to add a Combo Box to your form. Or click once with the left hand mouse button, and then draw one on the form. A combo box is a way to limit the choices your user will have. When a black down-pointing arrow is clicked, a drop down list of items appears.

What is list box and combo box in VB net?

Generally, a combo box is appropriate when there is a list of suggested choices, and a list box is appropriate when you want to limit input to what is on the list. A combo box contains a text box field, so choices not on the list can be typed in. The exception is when the DropDownStyle property is set to DropDownList.

Why do we use ComboBox in VB net?

The ComboBox control is used to display more than one item in a drop-down list. It is a combination of Listbox and Textbox in which the user can input only one item. Furthermore, it also allows a user to select an item from a drop-down list.


4 Answers

you are using an index which is zero based.

change this:

For i = 0 To ComboBox1.Items.Count

to this:

For i = 0 To ComboBox1.Items.Count - 1
like image 198
RonB Avatar answered Nov 15 '22 10:11

RonB


Your problem probably happens here:

s = Convert.ToString(ComboBox1.Items(i))

This doesn't return the value. It returns a string representation of the object at the given index, which in your case apparently is of type System.Data.DataRowView.

You would have to cast ComboBox1.Items(i) to the approbriate type and access its Value. Or, since its a DataRowView, you can access the values throgh the appropriate column names:

Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")

Nevertheless, first of all you should definitely close and dispose the connection, no matter whether the transaction fails or succeeds. This can be done in a finally block (option 1) or with a using statement (option 2).

Option 1

// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
    // ...
Catch ex As Exception
    Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
    con1.Close()
    con1.Dispose()
End Try

Option 2

// ...
Using con1 as New MySqlConnection(str)
    con1.Open()
    Try
        // ...
    Catch ex As Exception
        Lblmsg.Text = " Error in data insertion process....." + ex.Message
    Finally
        con1.Close()
    End Try
End using
like image 42
Dennis Traub Avatar answered Nov 15 '22 10:11

Dennis Traub


This also works!

        Dim stgTest = "Some Text"
        Dim blnItemMatched As Boolean = False

        '-- Loop through combobox list to see if the text matches
        Dim i As Integer = 0
        For i = 0 To Me.Items.Count - 1
            If Me.GetItemText(Me.Items(i)) = stgTest Then
                blnItemMatched = True
                Exit For
            End If
        Next i

        If blnItemMatched = False Then

            Dim stgPrompt As String = "You entered  '" & stgTypedValue & "',  which is not in the list."
            MessageBox.Show(stgPrompt, "Incorrect Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Me.Text = ""
            Me.Focus()

        End If
like image 23
DanW52 Avatar answered Nov 15 '22 10:11

DanW52


Even after long time back you will achieve this with simply by following

For Each item As Object In combx.Items

    readercollection.Add(item.ToString)

        Next
like image 26
mzonerz Avatar answered Nov 15 '22 08:11

mzonerz