2 Replies - 1203 Views - Last Post: 08 June 2012 - 08:39 AM Rate Topic: -----

#1 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 150
  • Joined: 22-September 09

Changing combobox text

Posted 08 June 2012 - 08:13 AM

Hi,

The following code fills the items in a combo box from a sql table. Then through a query I want to change the text in the combo box based on the column id.
The code runs w/o errors but the combo box doesn’t change the text. I just cannot find the property to make the combo work. I hope someone can give me a helping hand.

TIA

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'Load combo
        Dim db As New BD
        Dim con = db.myConnection
        Dim sql As String

        Try

            con.Open()

            sql = "SELECT id, name  FROM vbremindertype " _
            & "ORDER BY id"

            Dim cmd As New MySqlCommand(sql, con)
            Dim da As New MySqlDataAdapter(cmd)
            Dim ds As New DataSet

            da.Fill(ds)
            ' assign datasource to cbo  
            Me.cbotype.DataSource = ds.Tables(0)
            ' assign property to cbo DisplayMember    
            Me.cbotype.DisplayMember = ds.Tables(0).Columns(1).Caption.ToString

            da.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, _
                            "Error in fillig combo box", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try

        '===================
        'select combo item
        sql = "SELECT type " _
           & "FROM vbreminderdate WHERE id = (SELECT MAX(id) FROM vbreminderdate)"

        Try

            con.Open()

            Dim cmd As New MySqlCommand(sql, con)
            Dim dr As MySqlDataReader = cmd.ExecuteReader()
'combo cols: id, type want to show type base on id
            If dr.Read() = True Then
                MsgBox(dr.Item("type").ToString) 'id # is correct
                Me.cbotype.SelectedItem(0) = _
                    Me.cbotype.SelectedValue(dr.Item("Type")) 'here fails
            End If

            cmd.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, _
                            "Error reading.", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            con.Close()
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Changing combobox text

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 448
  • View blog
  • Posts: 3,101
  • Joined: 12-January 10

Re: Changing combobox text

Posted 08 June 2012 - 08:20 AM

What do you mean the combo box doesnt change the text? Are you saying that after you select the item inthe combo box it appears in a text box for editing and from there you want it to go back into the combo box?
Was This Post Helpful? 0
  • +
  • -

#3 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 150
  • Joined: 22-September 09

Re: Changing combobox text

Posted 08 June 2012 - 08:39 AM

The first part of the code reads the data from sql tabletype and fills the combo box.

Id type
1 Please select a type
2 Appointment
3 Anniversary
4 DOB

Per default, appears id 1 with the “Pls select a type” text.

The second part of the code reads tabledata where column type id has been stored. In the form, the textboxes are all filled up by the MySqlDataReader.Read function except the combobox. I want the text combobox reflect the data read based on its id.

EDIT:

Fixed it with this line:

Me.cbotype.SelectedIndex = dr.Item("Type") - 1


This post has been edited by ebolisa: 08 June 2012 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1