for loop with arraylist

buggiez

Member
Joined
Feb 7, 2006
Messages
18
Location
Singapore
Programming Experience
1-3
hi i have a question..

my subscribers has subscribed to several newsletters groups : Entertainment/Games/Shopping, which means that 1 subscriber can be subscribed to more than 1 newsletter.

when i create a newsletter, i want to select the list of subscribers based on the newsletter groups selected. (the groups are displayed in checkboxlists.)

and so i created a FOR LOOP, which means that,

FOR EACH checked group,

i will select the list of userids out, and place them inside an arraylist.

however, the problem is it only displays 1 record instead of 4.

i tried having the checkboxlist checked with 3 newsletters group,and the record becomes 3 instead of the total number (total users for entertainment + games + shopping) of 14.

why is this so?

thanks in advance for your replies! ^^
 
I'm not sure about others here but I'm not a mind reader and would need to see the code in order to know why you get the results you do :).

What is meant by newsletter group? Where do you get the userids? What is it like living in z?
 
oops im sorry.. each users can subscribe to more than 1 usergroups, and i would like to select the list of users out based on the usergroups. lets say:

selecting 10 users out from Entertainment group,
and 5 users out from Games group....etc

FIRSTLY, i will place the selected checkboxlist values into an arraylist,

VB.NET:
'news group
Dim item_NewsGroup As ListItem
Dim itemlist_NewsGroup As String = ""
For Each item_NewsGroup In cbl_subscriptions.Items
If item_NewsGroup.Selected = True Then
 
 
If arrayNewsGroup Is Nothing Then
arrayNewsGroup = New ArrayList
End If
 
arrayNewsGroup.Add(item_NewsGroup.Value)
Session.Item("arrayNewsGroup") = arrayNewsGroup
 
itemlist_NewsGroup &= item_NewsGroup.Text & "<br>"
End If
Next
'end of news group


and then select the users out according to the newsgroup. (which is the checked values from checkbox list)


VB.NET:
' select users according to newsgroup
arrayNews = Session.Item("arrayNewsGroup")
Dim i_News As Integer = 0
Dim a_News As String
For Each a_News In arrayNews
dr_News = dt_News.NewRow
 
Dim News As String = Convert.ToString(arrayNews.Item(i_News))
 
dt_News.Rows.Add(dr_News) ' add new rows
i_News = i_News + 1
 
Dim ds_News As New DataSet
Dim da_News As New SqlDataAdapter("SELECT * FROM userNewsGrpRel ug, NewsGrp n " _
& "WHERE ug.newsgrpid = n.id and newsgrpid = '" _
& News & "' ", cn)
cn.Open()
da_News.Fill(ds_News)
cn.Close()
 
 
Dim uid As String = ds_News.Tables(0).Rows(0)("userID")
 
 
If arrayUsers Is Nothing Then
arrayUsers = New ArrayList
End If
 
arrayUsers.Add(uid)
Session.Item("arrayUsers") = arrayUsers
 
 
Next


after having selecting out the list of users, i would want the userids to be stored into another db table..mailerlog.


VB.NET:
'to mailer log
Dim i_userid As Integer = 0
Dim a_userid As String
 
For Each a_userid In arrayUsers
dr_userid = dt_userid.NewRow
 
Dim userid As String = Convert.ToString(arrayUsers.Item(i_userid))
 
dt_userid.Rows.Add(dr_userid) ' add new rows
i_userid = i_userid + 1
 
Dim ds_userid As New DataSet
cn.Open()
Dim query_userid As String = "INSERT INTO mailerLog(userid, mailerid, posteddate, postedby) VALUES ('" _
& userid _
& "','" _
& id _
& "','" _
& Date.Now() _
& "','" _
& Session("subName") _
& "')"
Dim myAdapter_userid As New SqlDataAdapter(query_userid, cn)
myAdapter_userid.Fill(ds_userid)
cn.Close()
 
Next
'end to mailer log



yup so thats all. i believe there is some logic errors as i was not able to insert the actual list of users into the db. lets say i checked a total of 6 newsgroup, it'll only insert 6 userids (most probably the 1st userid in each newsgroup), instead of a total of maybe 43?

thanks alot! :)
 
Last edited by a moderator:
Back
Top