By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

display value of selected item in listbox to textbox

P: 25
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(txtDefine) 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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  2.         Dim myCmd As New MySqlCommand
  3.         Dim myReader As MySqlDataReader
  4.  
  5.         Call Connect()
  6.         With Me
  7.             STRSQL = "select definition from glossary where word_id =" & lstWord.SelectedIndex
  8.  
  9.             myCmd.Connection = myConn
  10.             myCmd.CommandText = STRSQL
  11.             myReader = myCmd.ExecuteReader
  12.  
  13.             While (myReader.Read())
  14.                 txtDefine.Text = myReader("definition")
  15.  
  16.             End While
  17.             myReader.Close()
  18.             myConn.Close()
  19.         End With
  20.     End Sub
  21.  
how can I fix this?, any help would be greatly appreciated. thanks in advance
Jan 18 '13 #1
Share this Question
Share on Google+
8 Replies


100+
P: 185
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.
Expand|Select|Wrap|Line Numbers
  1. If (myReader.Read())
Jan 18 '13 #2

Rabbit
Expert Mod 10K+
P: 12,383
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.
Jan 18 '13 #3

P: 25
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
Jan 19 '13 #4

Rabbit
Expert Mod 10K+
P: 12,383
There's nothing to show if the sample data above is incorrect.
Jan 19 '13 #5

P: 25
this is how I populated my listbox with values:
Expand|Select|Wrap|Line Numbers
  1. Dim myCmd As New MySqlCommand
  2.         Dim myReader As MySqlDataReader
  3.         Dim myAdptr As New MySqlDataAdapter
  4.         Dim myDataTable As New DataTable
  5.         Call Connect()
  6.         With Me
  7.             STRSQL = "Select word from glossary"
  8.             Try
  9.                 myCmd.Connection = myConn
  10.                 myCmd.CommandText = STRSQL
  11.                 myReader = myCmd.ExecuteReader
  12.                 While (myReader.Read())
  13.                     myReader.Close()
  14.                     myAdptr.SelectCommand = myCmd
  15.                     myAdptr.Fill(myDataTable)
  16.                     ListBox1.DisplayMember = "word"
  17.                     ListBox1.ValueMember = "word_id"
  18.                     If myDataTable.Rows.Count > 0 Then
  19.                         For i As Integer = 0 To myDataTable.Rows.Count - 1
  20.                             ListBox1.Items.Add(myDataTable.Rows(i)("word"))
  21.                         Next
  22.                     End If
  23.  
  24.                 End While
  25.             Catch ex As Exception
  26.                 MessageBox.Show(ex.Message)
  27.             End Try
  28.         End With
  29.  
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
Jan 20 '13 #6

Rabbit
Expert Mod 10K+
P: 12,383
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.
Jan 20 '13 #7

P: 25
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 :)
Jan 21 '13 #8

Rabbit
Expert Mod 10K+
P: 12,383
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.
Jan 21 '13 #9

Post your reply

Sign in to post your reply or Sign up for a free account.