Programming for Everybody: How to get value selected item from combobox in VB.NET-with source code

How to get value selected item from combobox in VB.NET-with source code

 


Imports System.Data.SqlClient


Public Class Form5


    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As New SqlConnection(" data source=.;initial catalog=sports1;integrated security=true")


        Dim cmd As New SqlCommand("select id,name from sports", con)


        Dim da As New SqlDataAdapter


        da.SelectCommand = cmd


        Dim table1 As New DataTable


        da.Fill(table1)


        Dim row As DataRow = table1.NewRow


        row(0) = 0

        row(1) = "Select sport"


        table1.Rows.InsertAt(row, 0)


        ComboBox1.DataSource = table1

        ComboBox1.DisplayMember = "name"

        ComboBox1.ValueMember = "id"








    End Sub


    

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

        Label1.Text = ComboBox1.SelectedValue


    End Sub

End Class

No comments:

Post a Comment