display value of selected item in listbox to textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumsay
    New Member
    • Jan 2013
    • 25

    display value of selected item in listbox to textbox

    hello,I have a simple problem with regards to my project. I'm doing a simple glossary. In my form, I have a textbox to search a word from the listbox, of course a listbox(lstWord ) populated with words stored in the database, and another textbox(txtDefi ne) to display the meaning of the selected word from the listbox. My problem is that when I select a word/item from the listbox it only displays the value of the second item onward and nothing displays when I select the first item. In the table in my database (example only):
    word_id word definition
    1 sample abc
    2 sample1 def
    3 sample2 xyz
    ..so my listbox displays sample,sample1, sample2. When I select sample the value should be abc but nothing appears,when I select sample1 its value displayed is abc, if sample2 the value became xyz..this is my code:
    Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim myCmd As New MySqlCommand
            Dim myReader As MySqlDataReader
    
            Call Connect()
            With Me
                STRSQL = "select definition from glossary where word_id =" & lstWord.SelectedIndex
    
                myCmd.Connection = myConn
                myCmd.CommandText = STRSQL
                myReader = myCmd.ExecuteReader
    
                While (myReader.Read())
                    txtDefine.Text = myReader("definition")
    
                End While
                myReader.Close()
                myConn.Close()
            End With
        End Sub
    how can I fix this?, any help would be greatly appreciated. thanks in advance
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    Two comments on the above code.
    1- How do you guarantee that the index in the list matches the ID on the database?
    2- Line 13, use if instead of while.
    Code:
    If (myReader.Read())

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Add 1 to the selected index. That starts at 0. Your ids start at 1. You really should be passing the id instead of the selected index because you never know if there may be gaps in the id field at some point.

      Comment

      • kumsay
        New Member
        • Jan 2013
        • 25

        #4
        thanks Anas Mosaad, I already changed the line 13 but about your first question..maybe that's what I couldn't figure out ^_^v
        @Rabbit
        sorry I have a mistake..the word_id above starts at 0 and not 1. Can you show me sir how to do it?,thanks

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          There's nothing to show if the sample data above is incorrect.

          Comment

          • kumsay
            New Member
            • Jan 2013
            • 25

            #6
            this is how I populated my listbox with values:
            Code:
            Dim myCmd As New MySqlCommand
                    Dim myReader As MySqlDataReader
                    Dim myAdptr As New MySqlDataAdapter
                    Dim myDataTable As New DataTable
                    Call Connect()
                    With Me
                        STRSQL = "Select word from glossary"
                        Try
                            myCmd.Connection = myConn
                            myCmd.CommandText = STRSQL
                            myReader = myCmd.ExecuteReader
                            While (myReader.Read())
                                myReader.Close()
                                myAdptr.SelectCommand = myCmd
                                myAdptr.Fill(myDataTable)
                                ListBox1.DisplayMember = "word"
                                ListBox1.ValueMember = "word_id"
                                If myDataTable.Rows.Count > 0 Then
                                    For i As Integer = 0 To myDataTable.Rows.Count - 1
                                        ListBox1.Items.Add(myDataTable.Rows(i)("word"))
                                    Next
                                End If
            
                            End While
                        Catch ex As Exception
                            MessageBox.Show(ex.Message)
                        End Try
                    End With
            I set the listbox value on the word_id and the listbox is displaying the words based on how I stored it on the database, so I think the index in the list matches the ID. I can say that the listbox absolutely has no problem. The only problem is that when I chose the sample, it displays nothing on the txtDefine, when I chose sample1, it displays abc and when i chose sample2 it displays def..let me correct my sample table above, it should be like this:
            word_id word definition
            0 sample abc
            1 sample1 def
            2 sample2 xyz

            correct me if I'm wrong sir, please?..thank you

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              If I were you, I'd double check the word_id in the table. Because if it's an autoincrement field, then those start at 1, not 0.

              Comment

              • kumsay
                New Member
                • Jan 2013
                • 25

                #8
                I've already checked the word_id in the database and it really is the problem ^_^v ..gosh!, I'm sorry for bothering. Thank you very much, it's now working :)

                I still have another problem regarding one of my projects about using checklistbox in vb.net but nobody seemed to know the answer..I've already posted it a week ago :(
                I would be very greatful if anybody could answer it.
                Anyway, thanks again :)

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Glad you got it working. That's why I said you should be passing the id instead of the selected index because you never know if there may be gaps in the id field at some point.

                  Comment

                  Working...