BP Forums
2 Listboxes and database - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+---- Thread: 2 Listboxes and database (/showthread.php?tid=580)



2 Listboxes and database - qazinasir - 07-20-2012

I want to build two listbox , listbox 1 and listbox2 ,and I have a datebase with table having ( Name, boilingpoint, density) , now on listbox1 I have given all the Name of the database , what I want is to select combination of items (Name) on listbox2 and use there boilingpoint and density in the coding in a formula .
how can i do that
attach is the preview of listbox to give you an idea what i am trying to do
regards
Nasir


Re: 2 Listboxes and database - brandonio21 - 07-20-2012

Ohhh! So you're making a little chemical mixing application, where you mix two chemicals and it forms a new solution with a new boiling point, right? How interesting!

Well, you already have the database setup, so all you would need to do is make a list of boiling points and a list of densities and go through each item in listbox2 and add the boiling points and densities to the list! Then use your formulas, and you should be all good!


Re: 2 Listboxes and database - qazinasir - 07-20-2012

exactly but how , can you give me a information in a coding format , I want to use each properties (density,boilingpoint) which can be read from database into my VB program .


Re: 2 Listboxes and database - brandonio21 - 07-20-2012

Well that's like.. the whole program you're asking me to code for you! That's a very large project. I don't have time at the moment, but here is a quick idea outline thing:

[code2=vbnet]Public Sub Calculate()
Dim boilingPoints As New List(Of Double)
Dim densities As New List(Of Double)

For Each item In ListBox2.Items
Dim itemName As String = item.ToString

Dim boilingPoint As Double
Dim density As Double
'NOW WE NEED TO SET THE VALUES OF BOILING POINT AND DENSITY
'FROM THE DATABASE USING THE CHEMICAL'S NAME

boilingPoints.Add(boilingPoint)
densities.Add(density)
Next

'HERE WE DO THE FORMULA THINGY USING THE TWO LISTS WE CREATED
End Sub[/code2]

I can elaborate more if need be when I get the time!


Re: 2 Listboxes and database - qazinasir - 07-21-2012

how to read the information of densities and boilingpoint from the database for selected items in listbox2 . mind it we are not adding the items to database , only reading the information and use that information for formula solving


Re: 2 Listboxes and database - brandonio21 - 07-21-2012

First, you want to add a reference to MySQL.Data to your project. Then, you want to:
[code2=vbnet]Imports MysQL.Data.MySqlClient[/code2]

Then, this is the code you're going to want to use to get the densities and boiling points from the MySQL database, or something like this:

[code2=vbnet]Dim conn As MySqlConnection = New MySqlConnection("MYSQL CONNECTION STRING")
Dim sqlQuery As String = "SELECT * FROM Chemicals WHERE Name='" & itemName & "'"
Dim command As New MySqlCommand
command.Connection = conn
command.CommandText = sqlQuery
Dim adapter As New MySqlDataAdapter
adapter.SelectCommand = command
Dim data As MySqlDataReader
conn.Open()
data = command.ExecuteReader

While data.Read
boilingPoint = CDbl(data(1))
density = CDbl(data(2))
End While

data.Close()
conn.Close()[/code2]


Re: 2 Listboxes and database - qazinasir - 07-22-2012

I have Made a database of (ID,Name.Age) and do the following codes , it doesn't work , Please guide me what can I do
Note :- I am not good in Programming

[code2=vbnet]Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Public Sub calculate()
Dim IDS As New List(Of Double)
Dim Names As New List(Of String)
Dim ages As New List(Of Double)

For Each item In ListBox2.Items
Dim itemName As String = item.ToString
Dim ID As Double
Dim age As Double
Dim conn As New SqlConnection("Data Source=C:\Users\QaziNasir\documents\visual studio 2010\Projects\NSfile\NSfile\NDatabase.sdf")
Dim sqlquery As String = "SELECT * From NSTable = '" & itemName & "'"
Dim command As New SqlCommand
command.Connection = conn
command.CommandText = sqlquery
Dim adapter As New SqlDataAdapter
adapter.SelectCommand = command
Dim Data As SqlDataReader
conn.Open()
Data = command.ExecuteReader
While Data.Read
ID = CDbl(Data(1))
Name = CDbl(Data(2))
age = CDbl(Data(3))
End While
Data.Close()
conn.Close()

IDS.Add(ID)
ages.Add(age)
Names.Add(itemName)
Next
End Sub
End Class[/code2]


Re: 2 Listboxes and database - brandonio21 - 07-22-2012

I couldn't help but notice this line:
[code2=vbnet]Dim conn As New SqlConnection("Data Source=C:\Users\QaziNasir\documents\visual studio 2010\Projects\NSfile\NSfile\NDatabase.sdf")[/code2]

Are you using a MySQL database? If not, what kind of database are you using?