user1947700
user1947700

Reputation: 69

VB.NET How to get row values from database to textbox (multiline)

I have this code (found it somewhere on the net, maybe even here on stackoverflow)

Dim SQL As String = "SELECT User FROM T1 WHERE User IS NOT NULL;"
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\db2.accdb")
    Dim command As New OleDbCommand(Sql, con)
    con.Open()

    Dim reader As OleDbDataReader = command.ExecuteReader()
    While reader.Read()
        TextBox1.Text = reader(0).ToString()
    End While
    reader.Close()
End Using

In my database I have 3 users but when I am running this code only the last one is showed in the textbox.

Let's say my table looks like this

User

one
two
three

But my textbox shows only "three"

How can I export and show all rows from column User and separate them with new line?

And if it is easy I would like to have a code for a list box. You know, like get column name (with sql query) and for every row make a new item in listview.

PS: the code works fine, just need to be adjusted. I am working with databases in vb.net for like 3 days, so my question might be easy, but still difficult for me.

Thank you.

Upvotes: 0

Views: 7663

Answers (1)

OldProgrammer
OldProgrammer

Reputation: 12159

  While reader.Read()
     TextBox1.Text &= reader(0).ToString() & Environment.NewLine 
  End While

Also make sure to set TextBox1.Multiline = true

Upvotes: 4

Related Questions