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: -
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
8 7261
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.
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.
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
There's nothing to show if the sample data above is incorrect.
this is how I populated my listbox with values: -
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
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.
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 :)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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 !!!
|
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;...
|
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...
|
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...
|
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()...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |