473,403 Members | 2,183 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,403 software developers and data experts.

vb 2008 get listbox items

25
hello, I am having a problem with getting items from a listbox. I have a form with 3 listboxes and a button. lstSymptoms is populated with symptoms from the database. When a symptom is selected, it is then listed in lstSelected. Now when diagnose button is clicked, it should get the symptoms from lstSelected and diagnose from the database table the illness where this symptoms is present, then display the result to lstIllness. My database table's structure is: f_id, illness, symptoms. So far here's my code:
Expand|Select|Wrap|Line Numbers
  1. Call Connect()
  2.  
  3.         Dim dt As New DataTable
  4.         Dim cmd As New MySqlCommand
  5.         Try
  6.             lstIllness.Items.Clear()
  7.             cmd.Connection = myConn
  8.             cmd.CommandText = "select ill from symptoms where sym = @symp"
  9.             cmd.Parameters.AddWithValue("symp", lstSelected.Items)
  10.             myReader = myCmd.ExecuteReader
  11.             If (myReader.Read()) Then
  12.                 myReader.Close()
  13.                 myAdptr.SelectCommand = cmd
  14.                 myAdptr.Fill(dt)
  15.                 lstIllness.DisplayMember = "ill"
  16.                 lstIllness.ValueMember = "ill"
  17.  
  18.                 For Each row As DataRow In dt.Rows
  19.                     lstIllness.Items.Add(row("ill"))
  20.                 Next
  21.                 Dim builder As New StringBuilder()
  22.                 builder.Append("select distinct ill from symptoms where ")
  23.                 For y As Integer = 0 To lstSelected.Items.Count - 1
  24.                     Dim parameterName As String = "@symp" & y.ToString()
  25.                     If y <> 0 Then
  26.                         builder.Append("and ")
  27.                     End If
  28.                     builder.Append(parameterName)
  29.                     builder.Append(" in (select sym from symptoms where ill = i.ill) ")
  30.                     cmd.Parameters.AddWithValue(parameterName, lstSelected.Items(y))
  31.                 Next
  32.                 cmd.CommandText = builder.ToString()
  33.             End If
  34.         Catch ex As Exception
  35.             MsgBox(ex.Message)
  36.         End Try
  37.         cmd = Nothing
  38.         myReader = Nothing
  39.         myConn.Close()
  40.         Call Disconnect()
  41.  
It has no error but when I click the diagnose button, nothing is happening. Please, can anybody tell me the problem with my code? I really need to finish this today, please help me. Thanks in advance, God bless
Feb 20 '13 #1
5 1483
Rabbit
12,516 Expert Mod 8TB
What does the final SQL string look like?
Feb 20 '13 #2
kumsay
25
I'm sorry sir I don't get you (not familiar with the terms), do you mean the result?
What I'm trying to do there is when diagnose button is clicked it will list in the lstIllness the illnesses that has a symptom present in the lstSelected. My table looks like this:
f_id | illness | symptoms
1 | fever | fever
2 | fever | cold
3 | fever | hot temperature
4 | fever | headache
5 | cold | cold
so if lstSelected have headache and cold, then lstIllness will list fever and cold.
Feb 20 '13 #3
Rabbit
12,516 Expert Mod 8TB
You're using SQL in your code but you don't know what SQL is? Where did you get the code from?
Feb 20 '13 #4
kumsay
25
I know what is SQL sir, its just that I'm just beginning to learn this things..I'm really sorry. I found the site where I got that code: http://stackoverflow.com/questions/1...95822#14695822. I added another listbox which is the lstSelected and made some modifications in the code. I thought its okay to do that, but obviously I was wrong and I don't know what to do. Can you please help me? :(
Feb 24 '13 #5
Mikkeee
94 64KB
kumsay, I looked at your code but there are a lot of issues with it. It looks to me like you're attempting to interact with a database but lack some basic programming logic. My intent here isn't to criticize you but you should probably start with a more basic project. Here are a few issues I can see right away.
  • Closing your connection at the end but not opening it anywhere.
  • Mismatched parameter name between commandtext and adding the parameter value.
  • Adding a collection of list box items to your parameter value.
  • Opening a reader and closing it for no apparent reason.
  • Building a StringBuilder, assigning it to the CommandText, and never using it.
Feb 24 '13 #6

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

Similar topics

6
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
3
by: Stimp | last post by:
I have a listbox of values that I populate from a database. I want the user to be able to re-order the list (by first selecting an item and then clicking 'up' or 'down' buttons) and then save...
3
by: Baren | last post by:
Hi! I have a generalized Stored Procedure to get the listbox items in a datareader.Then i am binding the datareader to the listbox. For different pages and different conditions i need to hide...
0
by: marcelo | last post by:
Could you help me out here. I need to accomplish this: I need to filter listbox items from by entering some characters to the textbox. But the problem is that items (files that are read from...
3
by: Brian Simmons | last post by:
Hi All, I search on codeproject and google but didn't find what I was looking for. Anybody know of a good implementation where you have 2 listboxes and you want to move items between the 2. ...
1
by: Refugnic | last post by:
I tried to fill a ListBox with a DataSource pointing to an ArrayList. It all works fine...up to one point. The ArrayList is dynamic, which means the contents of it change, during the course of...
6
by: kimiraikkonen | last post by:
Hi, I can read from a text file and insert text file's contents into a listbox whose format is line by line using this code: Dim reader As String reader = My.Computer.FileSystem.ReadAllText("c:...
1
by: divya | last post by:
Hello, I used the below code to move selected items between two listboxes Left and Right:- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPopup.aspx.cs" Inherits="TestPopup" %> ...
1
by: PaulBrains | last post by:
I was trying to sum up all the items of a listbox in Visual Studio 2005. I tried to loop (from 0 to ListBox.Items.Count - 1) as follows For j as Integer = 0 to ListBox.Items.Count - 1 ...
2
by: gnosys | last post by:
In ASP.Net 1.1 using C#, I'm trying to dynamically change the background colors of certain listbox items based on some criteria. For example:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.