Miguel Intoy
Miguel Intoy

Reputation: 113

Display all data from 1 column in database and display to a single textbox

I want display all data from 1 column in database and display to a single textbox and separated by comma.

Here is my code:

strsql = "Select value from lookup_table where group_id = " & desc_id
cmd = New SqlCommand(strsql, conn)        
Adapter = New SqlDataAdapter(cmd)
conn.Open()
Dim sms As New DataTable
Adapter.Fill(sms)
For i As Integer = 0 To sms.Columns.Count - 1
Label1.Text = sms.Rows(0)(i).ToString + ","
 Next

Upvotes: 0

Views: 198

Answers (2)

Shreekant
Shreekant

Reputation: 469

You are not appending the data but overwriting it.

Remove this line

Label1.Text = sms.Rows(0)(i).ToString + ","

Replace it with this

 Label1.Text = Label1.text + "," + sms.Rows(0)(i).ToString

Upvotes: 0

Peter Ksenak
Peter Ksenak

Reputation: 315

try that code

it shows all data in column 0

    strSql = "Select value from lookup_table where group_id = " & desc_id
    Dim conn As SqlConnection = New SqlConnection(DbCOnn)
    Dim cmd = New SqlCommand(strSql, conn)
    Dim Adapter = New SqlDataAdapter(cmd)
    conn.Open()
    Dim sms As New DataTable
    Adapter.Fill(sms)
    Label1.Text = ""
    For i As Integer = 0 To sms.Rows.Count - 1
        Label1.Text += sms.Rows(i)(0).ToString + ","
    Next

Upvotes: 1

Related Questions