i am working on a POS as a school project. im having a problem in getting the stocks, which is a record in my database.

this is what i need to do, i need to populate the combobox with drug names with suggests. when i choose a certain drug, its stocks will be displayed on a textbox as a reference.

this is what ive done so far:

Private Sub posform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        accountlbl.Text = mainmenu.namelbl.Text

        dbconnect()
        Dim suggest As New SqlCommand("select prodname from productinfotbl ", connect)
        suggest.CommandType = CommandType.Text

        adapt.SelectCommand = suggest
        adapt.SelectCommand.ExecuteNonQuery()
        adapt.Fill(dset, "suggests")

          ComboBox1.DataSource = dset.Tables("suggests")
        ComboBox1.DisplayMember = "prodname"
        ComboBox1.ValueMember = "prodname"
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest

i was able to populate the combobox with this but the error strikes when i put this code so i can fetch other data when i change the value of the combobox:

 dbconnect()

        Dim sql As New SqlCommand("select * from  productinfotbl where prodname='" & ComboBox1.Text & "'", connect)
        sql.CommandType = CommandType.Text
        adapt.SelectCommand = sql
        adapt.SelectCommand.ExecuteNonQuery()
        adapt.Fill(dset, "prods")
        dc()

        stocktbx.Text = dset.Tables("prods").Rows(0).Item("stocks")

it shows the ff error message:

An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code

If I understand you correctly, when the combobox is set to a certain drug, you want the stocks corresponding to that drug to be stored in a textbox.

Here is a solution:

'Create a dataview
dim dv as new dataview

'Associate the dataview to dst (Dataset table)
dv.Table = dst.Tables("Drugs")

'Data Row View object to query DataView object
Dim drv As DataRowView 

'Filter based on a combo box value selected
dv.RowFilter = "[Drug ID] = " & CInt(cboComboBox.SelectedValue)


'Retrieve my values returned in the result
For Each drv In dv
     Stock_Level = drv("Stocks") 
Next

thanks for the suggestion maurice, but my question is this: where should i put that code? i mean, which event?

In the combobox SelectedIndexChanged event, also include:

Stocks.Textbox.Text = Stock_Level

oh thanks maurice! that really saved me! :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.