|
-
Jul 22nd, 2004, 09:13 AM
#1
Thread Starter
Member
ListBoxes
How would I take results from a database and make ListItems out of them?
What I am trying to do is populate a listbox with database results. I can populate the listbox no problem, but when I try to add a value from listbox1 into listbox2, it's not going through. It has no index to go along with the value. How would I go about repairing this?
-
Jul 22nd, 2004, 09:19 AM
#2
Frenzied Member
post some code. are you using listBox.items.item(index) yadda yadda? how are you going about copying the values?
-
Jul 22nd, 2004, 09:47 AM
#3
Thread Starter
Member
Listbox code
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 need to add a index value along with this? Because right now, the values populate inside the listbox, but can't take the values from this listbox and add them to another listbox.
-
Jul 22nd, 2004, 09:58 AM
#4
Frenzied Member
VB Code:
ListBox2.Items.Add(ListBox1.Items.Item(index))
Yeah, you need to specify which item in box 1 to copy into box 2.
i hope that's what you're looking for.
-
Jul 22nd, 2004, 10:06 AM
#5
Frenzied Member
one other thing. You're not using the .Item property of the reader to add the items. I didn't know you could do that. Or is that a typo?
I've always done it like this:
-
Jul 22nd, 2004, 03:40 PM
#6
Thread Starter
Member
Reader.item
Actually, what you did just worked. I added Item to the reader, which worked.
I also had my code in the body then on top because I was able to get Sub Page_Load to work. It turned out that I had to delete the top highlighted yellow "Page language=VB" for it to work and it did.
Thanks for your help!!
Now, how would I get all of the items out of the listbox to store it into one variable?
I would like to use the selected e-mails to store into one variable so I can add it to the e-mail function.
-
Jul 22nd, 2004, 03:59 PM
#7
Frenzied Member
you can try this:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Add("hello", CheckState.Checked)
CheckedListBox1.Items.Add("goodbye", CheckState.Checked)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim emails As New ArrayList
emails.AddRange(CheckedListBox1.CheckedItems)
For Each s As String In emails
Console.WriteLine(s)
Next
End Sub
try that out by creating a new project, add a checked list box and abutton to the blank form and run it. that should give you an idea of how to do that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|