简体   繁体   中英

retrieving multiple data from one column using vb.net

I m new to asp.net is there any way that i could display different data in one row where the data is pulled from same column of another table? where the column is name hello and contains 3 types of data let`s say my code is like this:

    cmd1.CommandText = " SELECT hello FROM link where mode=@model and procedure =@procedure "
    cmd1.CommandType = System.Data.CommandType.Text
    Dim da1 As New SqlDataAdapter()
    da1.SelectCommand = cmd1
    Dim dt1 As New DataTable()
    da1.Fill(dt1)
    Dim myDataReader1 As SqlDataReader
    myDataReader1 = cmd1.ExecuteReader()
    Dim hello As String
    If myDataReader1.HasRows Then

        Do While myDataReader1.Read()
            hello= myDataReader1("hello").ToString()

        Loop
        lblhello.Text = hello
    Else
        lblhello.Text = ""
    End If


    myDataReader1.Close()  

my data output should be like:

hihi,yoyo,heyhey

but i ended up getting

heyhey

only please help me on this issue

You are overwriting the hello string on every iteration in the Do while loop. Therefore you are displaying only the last value as it is that is contained in that variable.

You need to concatenate the values on each iteration. You also need to add a , between each value to get the desired output.

Change:

hello = myDataReader1("hello").ToString()

to

hello += myDataReader1("hello").ToString() & ","

or ...

hello = hello & myDataReader1("hello").ToString() & ","

EDIT: As the above solution will add an extra , at the end of the last added value to hello , you will need to remove it before showing the final string.

Replace

lblhello.Text = hello

by

lblhello.Text = hello.Text.Substring(0, hello.Text.Length - 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM