i have 1 listbox that contain : cloth, jeans, bra (directly from database) and 1 disabled textbox. The purpose is, when user click cloth or jeans or bra in listbox, textbox should show the price automatically, how can i do that??

this is my code to add item in list box :

Try
                conn.Open()

                Dim command As New MySqlCommand
                Dim sqlquery As String = "SELECT * FROM collection"
                Dim adapter As New MySqlDataAdapter
                Dim data As MySqlDataReader

                command.CommandText = sqlquery
                command.Connection = conn
                adapter.SelectCommand = command
                data = command.ExecuteReader

                While data.Read()
                    ListBox1.Items.Add(data(0).ToString)
                End While

                data.Close()
                conn.Close()

            Catch ex As Exception
                MsgBox("System Error, please try again", vbCritical)
            End Try

thx before for advice

Then based on the selected item u need to run a query to get the corresponding price and who it on textbox

Then based on the selected item u need to run a query to get the corresponding price and who it on textbox

code please.. sir!! :(

i really have no idea about it.

Edited 4 Years Ago by artemix22: n/a

The items in database from where ur getting the list is having primary key associated? Then bind the dataset using Displaymember and valuemebmer first.

Create a datatable:

'Where table name is the table in the db.
 Dim dt As DataTable = ds.Tables("tablename")

 Dim newRow As DataRow
'Setting up the table for the data
 newRow = dt.NewRow()
'You should replace "column1" and "column2" with the column name in your db.
 newRow("column1") = TextBox1.Text
 newRow("column2") = TextBox2.Text
 dt.Rows.Add(newRow)

'Updating db
da.Update(ds, "tablename")

Then fill the listbox.

Then loop through and check between the values. If the value matches column one, populate the text box from column two.

SOLVED!!

Then based on the selected item u need to run a query to get the corresponding price and who it on textbox

need two hour for me to make it happen dude.. but.. thx anyway, you has open my mind :)

i still use my first code to add data to ListBox, and then i put some code like below in SelectedIndexChanged event for Listbox :

Try
            conn.Open()
            Dim command As New MySqlCommand
            Dim sqlquery As String = "SELECT * FROM collection WHERE product_name = '" & ListBox1.SelectedItem & "';"
            Dim adapter As New MySqlDataAdapter
            Dim data As MySqlDataReader

            command.CommandText = sqlquery
            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader

            While data.Read
                TextBox1.Text = data("price")
            End While

            data.Close()
            conn.Close()

        Catch ex As Exception
            MsgBox("System Error, please try again", vbCritical)
        End Try

Create a datatable:

'Where table name is the table in the db.
 Dim dt As DataTable = ds.Tables("tablename")

 Dim newRow As DataRow
'Setting up the table for the data
 newRow = dt.NewRow()
'You should replace "column1" and "column2" with the column name in your db.
 newRow("column1") = TextBox1.Text
 newRow("column2") = TextBox2.Text
 dt.Rows.Add(newRow)

'Updating db
da.Update(ds, "tablename")

Then fill the listbox.

Then loop through and check between the values. If the value matches column one, populate the text box from column two.

thx for your code.. i will try it with another approach :)

This question has already been answered. Start a new discussion instead.