listbox to textbox


  • I am using asp and vb to populate a listbox from a SSMS SQL database table. The table contains names and emails. The listbox populates the name just fine. I want to select a name and have it populate 2 textboxes with name and email without using button if possible.  I have tried everything like txtName.text=lbxnames.selecteditem.text and value but the only 1 that works is index but I don't want number. i have tried if statements and for statements but nothing seems to work. I have also tried coding it to database. Below is code to fill listbox and code to fil textbox.

        This code fills listbox
    Private Sub FetchName()
            SQL.AddParam("@name", "%" & txtName.Text & "%")
            SQL.ExecQuery("SELECT * " &
            "FROM Contacts " &
            "WHERE  Name LIKE @name;")
            If SQL.HasException(True) Then Exit Sub
            For Each r As DataRow In SQL.DBDT.Rows
        End Sub
    This code is supposed to fill textbox from listbox doesn't work
    Private Sub LbxNames_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LbxNames.SelectedIndexChanged
            SQL.ExecQuery("SELECT   * " &
                          "FROM Contacts " &
                          "WHERE Name = '" + LbxNames.SelectedItem.Text + "';")
            If SQL.RecordCount < 1 Then Exit Sub
            For Each r As DataRow In SQL.DBDT.Rows
                txtName.Text = r("Name")
                txtTo.Text = r("Email")
        End Sub
    Any name and email will populate listbox.
    if there is anything else let me know.

    Senin, 02 Juli 2018 16.34

Semua Balasan

  • Hello bobo040,

    This forum(Excel for Developers) is for development issue related to Excel Object Model and I think your issue is more related to Asp.Net. So I would suggest you post your issue on for help and for the currently thread, I will move it to Off-Topic  forum.

    Thanks for understanding,

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Selasa, 03 Juli 2018 08.56