Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 Listboxes and database
#1
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


Attached Files Thumbnail(s)
   
#2
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!
My Blog | My Setup | My Videos | Have a wonderful day.
#3
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 .
#4
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!
My Blog | My Setup | My Videos | Have a wonderful day.
#5
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
#6
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]
My Blog | My Setup | My Videos | Have a wonderful day.
#7
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]
#8
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?
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  VB2010 & MS Access Database kismetgerald 3 14,644 01-10-2012, 10:35 PM
Last Post: kismetgerald
  others can't connect to online database Stefanvp 3 14,503 08-14-2011, 09:46 PM
Last Post: brandonio21
  connect to online database elnashar77 3 14,669 08-09-2011, 01:27 PM
Last Post: brandonio21
  DataBase - Login - Not Working Murcs 4 17,061 07-02-2011, 09:46 AM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)