AboutSyed Rizwan Muhammad Rizvi Expertise I can answers questions regarding web based and desktop based programming in VB.Net. Which can include SOAP, XML, Custom Controls, COM Interoperability etc.
Experience Have been working in this specific area for last 2 years previously I was a VB 6 Developer with experties in other languages as well. Total 10 years of programming experience.
Question QUESTION: hello, I am new to VB.Net.
I have written code to pass a SQL query, but I need to store the results in an array. I don't know the VB.Net substitute for the VB code rs.getrows
here is the gist of the code.
Import System.Data.SqlClient
Private Sub getdata()
Dim sqlcon as new Sqlconnection(dbstring)
sqlcon.Open()
Dim sqlcom as new SqlCommand
sqlcom.Connection=sqlcon
sqlcom.CommandType=CommandType.Text
sqlcom.CommandText="Select ID from Table"
Dim Dr as SqlDataReader
Dr=sqlcom.ExecuteReader
Dr.Read()
''''Code needed here to store results into array'''
sqlcon.close()
sqlcom.connection=Nothing
End Sub
In VB is used ADODB , the code was
Private Sub getdata()
Dim cn as New ADODB.Connection
Dim rs as New ADODB.RecordSet
Dim a() as Integer
cn.open dbstring ''''already defined and initialized
rs.open "Select ID from Table" , cn, adOpenStatic, adLockReadOnly
If Not rs.EOF then
a= rs.GetRows
end if
cn.close
rs.close
set cn=nothing
set rs=nothing
End Sub
I can read all the data in dr.Read() by using msgbox(dr(0))
but I don't know how to copy the values to an integer array.
thank you for your help
ANSWER: Hi the exact method is not supported, you can work on with various work arounds though, e.g.
While Dr.Read
MsgBox Dr(0)
End While
Let me know if you need further help.
---------- FOLLOW-UP ----------
QUESTION: Thank you. What I want to do is get all the ID values from the table using this query, and then store it in an integer array, then generate a random ID (from those values) using the Rnd(). But I still can't copy the Dr.Read() to an integer array.
Or how to convert dr.Read() values to a compatible format.
How can I store these values? If you have any other suggestions to get a random ID number from the retrieved data, then please let me know.
Thank you again
Answer Try:
Dim intCol As New Collection
While dr.Read
intCol.Add(dr(0), dr(0))
End While
Here you are using a collection instead of an array I think you should be able to achieve what you want using Collection as well.