Display name when id is selected from the combobox

By James J

Display name in textbox when id is selected from the combobox...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Conn As SqlConnection = New SqlConnection("connection string")
        
Dim dr As SqlDataReader
        ComboBox1.Items.Clear()
        
Try
            Conn.
Open()
            
Dim cmd As SqlCommand = New SqlCommand("Select StudentId From Student", Conn)
            dr = cmd.
ExecuteReader
            While dr.Read()
                 ComboBox1.
Items.Add(dr.GetValue(0).ToString())
             
End While
            dr.
Close()
            Conn.
Close()
        
Catch ex As Exception
             MessageBox.
Show(ex.ToString())
        
End Try
ComboBox1.
SelectedIndex = 0
End Sub


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Conn As SqlConnection = New SqlConnection("connection string")
        
Dim dr As SqlDataReader
        Try
            Conn.
Open()
            
Dim cmd As SqlCommand = New SqlCommand("Select Name From Student Where StudentId=" & ComboBox1.SelectedItem.ToString() & "", Conn)
            dr = cmd.
ExecuteReader
            While dr.Read()
                 TextBox1.
Text = dr.GetValue(0).ToString()
             
End While
            Conn.
Close()
        
Catch ex As Exception
             MessageBox.
Show(ex.ToString())
        
End Try
    
End Sub

Display name when id is selected from the combobox  (622 Views)