Sunday, March 11, 2012

Database results stored in a Array

How would I go about storing (1 column) database results in a Array?

And then how would I go about extracting the elements from the Array?What, you mean other than by loading it in a recordset/dataset and then looping through the data populating an array? I presume you wanna do this in an application, not SQL Server?

Because T-SQL does not support array structures as far as I know (unless you count the table variable as an array).

Some more info, please. :-)|||My problems is that I am trying to load e-mail values (from a database) into a listbox. The problem I'm facing is that when the e-mails are loaded into the listbox right now, they do not have index values. I was thinking of storing these results in a array so I could have an index and value together e.g. ArrayName( "1", "name@.domain.com").

If there would be an easier way to do this, then I'd like to hear about it. Eventually, I want to populate listbox1 with all the e-mails from the database and then use an "Add" button to insert the highlighted e-mail into listbox2. From there, I would have my chosen e-mails in listbox2 that would get an e-mail from me.

Would I still use a recordset/dataset? If so, how would I add indexes to make each value unique? Or would I have to convert the recordset/dataset into ListItems so that they would have indexes and values together?|||


Dim conPubs As SqlConnection
Dim cmdSelectAuthors As SqlCommand
Dim dtrAuthors As SqlDataReader
conPubs = New SqlConnection(ConfigurationSettings.appSettings("STDConnect"))
conPubs.Open()
cmdSelectAuthors = New SqlCommand("Select * from Email", conPubs)
dtrAuthors = cmdSelectAuthors.ExecuteReader()
While dtrAuthors.Read()
Listbox1.Items.Add(dtrAuthors("email"))
End While
dtrAuthors.Close()
conPubs.Close()

Would I have to add an index value to the listbox along with the "email" value? Right now, it populates the listbox1 but then can't select anything to add into listbox2.|||What I think is if you want to add some selected items from a list box to another list box, you could do it directly.

Read through (Traverse) the ListBox1. Write an "if" statement which selects only the selected values and add them directly to the ListBox2.

This is what I'm saying.

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
ListBox2.Items.Add(ListBox1.Items(i).Text)
End If
Next
End Sub

If you want to use a key, value pair then you might have to use a Hash Table.

No comments:

Post a Comment