1
Dim sql As String = "Select ProductID From OrderDetail Order By ProductID Desc"
    Dim command As New SqlCommand(sql, connection)
    Dim reader1 As SqlDataReader = command.ExecuteReader()

How do i store all the productid that i have retrieve into an array?

2 Answers 2

2
Dim list As New List(Of Integer)

Using reader As SqlDataReader = command .ExecuteReader()
    While reader.Read()
        list.Add(reader.GetInt32(reader.GetOrdinal("ProductID")))
    End While
End Using
'check  list.ToArray() now

EDIT : But, Instead of return an Array, i Would return a Generic List of Integers (if you only want to return the ProductId) or A List of ProductClass objects

Private Function GetProductIDs() As IList(Of Integer)

    Dim list As New List(Of Integer)
    Dim conStr = "write your connection string here"

    Using connection As New SqlConnection(conStr )
        Dim sql As String = "Select ProductID From OrderDetail Order By ProductID Desc"
        Dim command As New SqlCommand(sql, connection)
        Using reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
                list.Add(reader.GetInt32(reader.GetOrdinal("ProductID")))
            End While
        End Using
    End Using

    Return list

End Function

EDIT 2 : As per the comment, To retrieve an put in the Text of a label, you can do this

Dim str As String
str = String.Join(",", GetProductIDs())
Label1.Text=str;

Assuming Label1 is theI D of your label control.The String.Join method will return a string of ProductId's seperated by comma like "1,2,6,7"

Sign up to request clarification or add additional context in comments.

4 Comments

How do i retrieve all the values from the list to a label.text?
'While reader.Read() Sum.Text = (reader("ProductID")) End While Loop While reader.NextResult() '
Hey, how do i get the first 3 items from the list?
@FredHomme: Either you can update your query to return the TOP 3 items or apply the TAKE function on the list we have
0
SQLdr = SQLCmd.ExecuteReader 'Gets Data

While dr.Read() 'While Data is Present        
      MsgBox(dr("Column Name")) 'Show data in a Message Box
End While

Loop While SQLdr.NextResult() 'Move to the Next Record

http://www.daniweb.com/software-development/vbnet/code/216920/sql-in-vb.net

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.