Hi to all
first to say I am beginner in programming so please dont mind for mistakes I could write here :)

I have this problem, on the form I use one combobox with Data Bound items, Data source, Display member & Value member.
Besides this combobox I have textbox with multiline turned ON.
My code should write values from specific table in Textbox when I sellect ID on the combobox. Actually this code works but not in a way I want

Me.TextBox2.Text = ComboBox2.SelectedValue

I would like to populate texbox from other columns as well. For my app I need one texbox in which I can see five lines of data, Company name, Address line1 & Address line2 & Address line3 & Address line4 . It's important to get data from different columns and each column in a new line.

Recommended Answers

All 3 Replies

You should use DataRowView class to retreive item and value from comboBox:

Dim view As DataRowView = comboBox1.SelectedItem
Dim value As Object = view("ColumnNameForValueMember")
Dim item As Object = view("ColumnNameForDisplayMember")
textBox1.Text = item.ToString()

Put this code into comboBox SelectedIndexChanged event.

Is this what u r looking for???

Imports System.Data.SqlClient

Public Class Form8

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Openconnection As String
        Openconnection = Open_DB_Connection("form7 - Button1 click")
        Dim da As New SqlDataAdapter("SELECT Distinct Doctorid FROM DoctorRegister", Connection)
        Dim dt As New DataTable
        da.Fill(dt)
        With ComboBox1
            '  .Items.Add("Select")
            .DataSource = dt
            .DisplayMember = "Doctorid"
            .ValueMember = "Doctorid"
            .SelectedIndex = 0
        End With
        Debug.Print(ComboBox1.SelectedValue.ToString)
        Dim Closeconnection As String
        Closeconnection = Close_DB_Connection("form7 - Button2 click")
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim Openconnection As String
        Openconnection = Open_DB_Connection("form7 - Button1 click")
        Dim myCommand As SqlCommand
        myCommand = New SqlCommand("SELECT * FROM [DoctorRegister]  where DoctorID='" + ComboBox1.Text + "'", Connection)
        Dim reader As SqlDataReader = myCommand.ExecuteReader
        While reader.Read
            TextBox1.Text = reader("FirstName") + Environment.NewLine + reader("LastName") 
            'insert number of data u need in text box with Environment.NewLine 
        End While
        reader.Close()
        Dim Closeconnection As String
        Closeconnection = Close_DB_Connection("form7 - Button2 click")
    End Sub
End Class

The first code works for me, now I just need to add NULL value handle code and that is it...

Thanks a lot

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.