I have an sql table called Stock with two fields called StockID and StockCode.
I want the user to select the stockcode from a combo which in turn populates the
stockid for that item into a lable on my form.
I have already populated the stockcode into the combo but dont know how to compelte the rest.
I have only been using Visual Studio for about 4 days, hope there is someone out there with the knowledge to help a newbie.
Imports System.Data.SqlClient
Public Class cbo2
Private Sub cbo2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connString As String = "Data Source=localhost;Initial Catalog=invsystem;User ID=mbish;Password=mbish"
Dim conn As New SqlConnection(connString)
' fillComboBox1()
Dim strSQL As String = "Select * From Stock"
Dim DA As New SqlDataAdapter(strSQL, conn)
Dim DS As New DataSet
DA.Fill(DS, "Stock")
'Create and populate the DataTable to bind to the ComboBox:
Dim dt As New DataTable
dt.Columns.Add("Stockcode", GetType(System.String))
dt.Columns.Add("StockID", GetType(System.String))
' Populate the DataTable to bind to the Combobox.
Dim drDSRow As DataRow
Dim drNewRow As DataRow
For Each drDSRow In DS.Tables("Stock").Rows()
drNewRow = dt.NewRow()
drNewRow("StockCode") = drDSRow("Stockcode")
drNewRow("StockID") = drDSRow("StockID")
dt.Rows.Add(drNewRow)
Next
'Bind the DataTable to the ComboBox by setting the Combobox's DataSource property to the DataTable. To display the "Description" column in the Combobox's list, set the Combobox's DisplayMember property to the name of column. Likewise, to use the "Code" column as the value of an item in the Combobox set the ValueMember property.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
With ComboBox1
.DataSource = dt
.DisplayMember = "StockCode"
.ValueMember = "StockID"
.SelectedIndex = 0
End With
End Sub
End Class