locked
TextBox's value that associate with combo box RRS feed

  • Question

  • How to display textBox's value that associate with combo box? for example, I have a combo box for selecting EmployeeID and I want the text box will display EmployeeName.

    One more, when setting comboBox's DisplayMember to EmployeeName and ValueMember to EmployeeID, I always get an error msg "Cannot set column 'EmployeeID'. The value violates the MaxLength limit of this column." when moving to another record.

    Could anyone helps me on this?
    Best regard


    Wednesday, August 20, 2008 10:58 AM

Answers

  •  
    Public Class Form1  
     
        'The name of the combobox is ComboBox1. You want the SelectedIndexChanged event.  
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            'Name of the textbox is TextBox1  
            Me.TextBox1.Text = Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)  
        End Sub  
    End Class 

    "Nothing Ventured, Nothing Gained"
    Wednesday, August 20, 2008 12:50 PM
  • Duong Nguyen 1973 said:

    How to display textBox's value that associate with combo box? for example, I have a combo box for selecting EmployeeID and I want the text box will display EmployeeName.

    Hi Duong,

    Please check the code sample:

    Imports System.Data.OleDb  
     
    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
     
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Employee.mdb")  
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM EmployeeTable", con)  
            con.Open()  
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)  
            Dim myDataSet As DataSet = New DataSet()  
            myDA.Fill(myDataSet, "MyTable")  
     
            ComboBox1.DataSource = myDataSet.Tables("MyTable").DefaultView  
           
            ComboBox1.DisplayMember = "EmployeeID" 'EmployeeID info will be displayed in ComboBox items   
            ComboBox1.ValueMember = "EmployeeName" 
     
            con.Close()  
            con = Nothing 
     
        End Sub 
     
        ' Select EmployeeID from ComboBox, and corresponding EmployeeName will be displayed on TextBox  
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            TextBox1.Text = ComboBox1.SelectedValue.ToString  
        End Sub 
     
    End Class 


    Duong Nguyen 1973 said:

    One more, when setting comboBox's DisplayMember to EmployeeName and ValueMember to EmployeeID, I always get an error msg "Cannot set column 'EmployeeID'. The value violates the MaxLength limit of this column." when moving to another record.

    I cannot repro this issue. I don't think such issue would occur when only retrieving data from database.

    Please check these similar issues for reference:
    "Cannot set column xxx . The value violates the MaxLength limit of this column"

    http://forums.asp.net/t/1130944.aspx
    http://forums.asp.net/p/1172903/1967729.aspx


    Best regards,
    Martin Xie

    Monday, August 25, 2008 6:52 AM

All replies

  •  
    Public Class Form1  
     
        'The name of the combobox is ComboBox1. You want the SelectedIndexChanged event.  
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            'Name of the textbox is TextBox1  
            Me.TextBox1.Text = Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)  
        End Sub  
    End Class 

    "Nothing Ventured, Nothing Gained"
    Wednesday, August 20, 2008 12:50 PM
  • Duong Nguyen 1973 said:

    How to display textBox's value that associate with combo box? for example, I have a combo box for selecting EmployeeID and I want the text box will display EmployeeName.

    Hi Duong,

    Please check the code sample:

    Imports System.Data.OleDb  
     
    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
     
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\Employee.mdb")  
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM EmployeeTable", con)  
            con.Open()  
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)  
            Dim myDataSet As DataSet = New DataSet()  
            myDA.Fill(myDataSet, "MyTable")  
     
            ComboBox1.DataSource = myDataSet.Tables("MyTable").DefaultView  
           
            ComboBox1.DisplayMember = "EmployeeID" 'EmployeeID info will be displayed in ComboBox items   
            ComboBox1.ValueMember = "EmployeeName" 
     
            con.Close()  
            con = Nothing 
     
        End Sub 
     
        ' Select EmployeeID from ComboBox, and corresponding EmployeeName will be displayed on TextBox  
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged  
            TextBox1.Text = ComboBox1.SelectedValue.ToString  
        End Sub 
     
    End Class 


    Duong Nguyen 1973 said:

    One more, when setting comboBox's DisplayMember to EmployeeName and ValueMember to EmployeeID, I always get an error msg "Cannot set column 'EmployeeID'. The value violates the MaxLength limit of this column." when moving to another record.

    I cannot repro this issue. I don't think such issue would occur when only retrieving data from database.

    Please check these similar issues for reference:
    "Cannot set column xxx . The value violates the MaxLength limit of this column"

    http://forums.asp.net/t/1130944.aspx
    http://forums.asp.net/p/1172903/1967729.aspx


    Best regards,
    Martin Xie

    Monday, August 25, 2008 6:52 AM