Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

Create list from SqlDataReader

Hello experts,

How can I create a list from an ADO query that will show in a response.write format?
In my example below I don't get any text just a string like below:
"System.Collections.Generic.List`1[System.String]"
Basically all I'm trying to do is to return all records in the list table and write contents as a list.
Any help would be greatly appreciated.


Dim emListSelect As String
            emListSelect = "SELECT  list.id, list.type, list.store, list.product, list.qty, list.price, list.fk_Cat, list.listDTE, "
            emListSelect &= "ProdStore.store_name FROM list INNER JOIN ProdStore ON list.store = ProdStore.store_id "
            emListSelect &= "ORDER BY list.listDTE DESC"
            Dim emListConnString As String = WebConfigurationManager.ConnectionStrings("ShopConnection").ConnectionString
            Dim emListConn As New SqlConnection(emListConnString)
            Dim emListCommand As New SqlCommand(emListSelect, emListConn)

            Dim readerList As SqlDataReader
            Dim returnData As List(Of String) = New List(Of String)
            emListConn.Open()
            readerList = emListCommand.ExecuteReader()
            While readerList.Read()
                returnData.Add(readerList("product").ToString())
            End While
            Response.Write(returnData.ToArray())
            emListConn.Close()

Open in new window

0
Phosphor
Asked:
Phosphor
  • 2
1 Solution
 
Evan CutlerVolunteer Chief Information OfficerCommented:
Response.Write(returnData.ToArray())

You can create an IList<String> and insert your data into that.

ie. look at this:  http://www.java2s.com/Code/VB/Generics/GenericStringList.htm

Then the IList can be used in a for-each statement and done with what you want.
You can even do a join and output as a comma-delineated list.

0
 
PhosphorAuthor Commented:
Thanks anyway arcee123, I figured out what I needed to do:

Created a bulleted list named bl and used that as a control to facilitate
a RenderControl to a string.

Dim bl1 As New BulletedList
            Dim readerList As SqlDataReader
            readerList = emListCommand.ExecuteReader()
            Do While readerList.Read()
                Dim newItem As New ListItem()
                newItem.Text = readerList("product")
                newItem.Value = readerList("product").ToString()
                bl1.Items.Add(newItem)
            Loop
            emListConn.Close()
            Dim SB As New StringBuilder()
            Dim SW As New StringWriter(SB)
            Dim htmlTW As New HtmlTextWriter(SW)
            bl1.RenderControl(htmlTW)
            Dim htmlList As String = SB.ToString()

I tried it several ways but this was drop-dead easy.
I didn't mention that the end use was to email the list on an inserted function.
0
 
PhosphorAuthor Commented:
arcee 123's reply was pointing to a block of java code.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now