473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

display value of selected item in listbox to textbox

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
8 7261
Anas Mosaad
185 128KB
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
12,516 Expert Mod 8TB
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
kumsay
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
12,516 Expert Mod 8TB
There's nothing to show if the sample data above is incorrect.
Jan 19 '13 #5
kumsay
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
12,516 Expert Mod 8TB
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
kumsay
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
12,516 Expert Mod 8TB
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

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

Similar topics

4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
1
by: tupolev | last post by:
I have successfully created a datagrid with a combobox added in a column of the datagrid, I also have written an event that works when you select an other item in the combobox. Know: how can I...
3
by: amber | last post by:
Hello again : How do I specify which item in a listbox I want selected The listbox is already populated, and if the user types in a value into a textbox, I want that value to be highlighted in...
3
by: peter.mosley | last post by:
I've tried googling for the answer to this problem, without any luck. I'm sure the truth must be out there somewhere! I have a multiselect listbox populated with many items (set by the RowSource...
16
Ranjan kumar Barik
by: Ranjan kumar Barik | last post by:
Hello !!! I am Ranjan. can the selected item of a combo box be displayed on the same page. I mean when I just click the item of a combo box it will atonce displayed on the page. Thanks !!!
3
by: mlevit | last post by:
Hi, I need to obtain the value of the selected item from a combo box. BEGIN IF :LOGIN_BLOCK.USER_TYPE = 'Student' THEN :GLOBAL.student_id := :LOGIN_BLOCK.ID;...
1
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post by:
Hi, I have a problem whereby I have a gridview bound to a dataset with a large number of items, However when I select an item near the bottom by using the scrollbar, when the gridview gets...
3
by: aashishn86 | last post by:
Hi all, I am new to ASP, i am making a page on which i have a list box and a textbox. I have dynamically populated the list box from data on the server, what i want it that , when i select a option...
7
by: ieda | last post by:
I want to display information when value in listbox selected. Below is my code:- This code to get value option in listbox <? $query = "select courseTitle from training where startDate > NOW()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.