Connecting Tech Pros Worldwide Forums | Help | Site Map

casting of object to retrieve individual value from arraylist

Newbie
 
Join Date: Sep 2009
Posts: 6
#1: Sep 29 '09
Hi, i am having a problem as im new to c#. I did an arraylist and store my "uid" and want to put it into a row of pictures

pic 1 , pic 2 , pic 3, pic 4

this is my code for putting the "uid" the arraylist

pofile2.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. ArrayList list2 = new ArrayList();
  2. //query to get uid
  3. thesql = "select * from Student where caregroupID= @CareGroupID";
  4.  
  5. using (SqlCommand cmd = new SqlCommand(thesql, con))
  6. {
  7.   cmd.Parameters.AddWithValue("CareGroupID", Session["caregroupID"].ToString());
  8.   con.Open();
  9.  
  10.   using (SqlDataReader reader = cmd.ExecuteReader())
  11.   {
  12.       while (reader.Read())
  13.       {
  14.           list2.Add(reader["uid"].ToString());
  15.       }
  16.   }
  17.   Session["horse"] = list2;
  18.   con.Close();
  19. }
  20.  
I understand my Session becomes an object and so i have to cast it back.
This is my aspx page where i tried to cast it.
Expand|Select|Wrap|Line Numbers
  1. <%ArrayList mylist = (ArrayList)Session["horse"];%> 
  2. <!--fb:fan profile_id="171381255280" name="caregroup" stream="0" connections="8" width="250"></fb:fan-->
  3. </form>
  4. <div>
  5.     <table cellpadding=0 cellspacing=11>
  6.     <tr>
  7.         <%for (int i = 0; i < 8; i++)%>
  8.       <%{ %>
  9.         <td> 
  10.         <fb:profile-pic uid="<%=mylist[i].ToString()%>" facebook-logo="false" width="50" height="50"linked="true" />
  11.         </td>
  12.       <%}%>
  13.     </tr>
  14.     </table>
  15. </div>
  16.  
But it seems like i can't put the cast at my ASPX page. The error states the namespace arraylist does not exist, Any idea what other solution can be used?

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


Well your error could probably be fixed by saying System.Collections.ArrayList
(or adding the System.Collections namespace for it)

However, if you are interested in display tabluar data in a webpage, I recomend looking up the DataTable object(and the SqlDataAdapter to fill it), then using a GridView object to handle the table building
Newbie
 
Join Date: Sep 2009
Posts: 6
#3: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


where do i put the System.Collections.ArrayList
in my ASPX page?
Like <%System.Collections.ArrayList ;%>

Sorry Im really new to this
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


I would go to the code file, since the exception is probably on the line:
ArrayList list2 = new ArrayList();
And put the cursor over the first ArrayList word. A little red line will probably appear under the last letter. If you move over it, a drop down will appear allowing you to add the namespace if you want?
Newbie
 
Join Date: Sep 2009
Posts: 6
#5: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


There is no error in the ArrayList list2 = new ArrayList(); i have all the namespace needed for the code file. But it is in my ASPX page where there is error in <%ArrayList mylist = (ArrayList)Session["horse"];%>.

Hope you guys can understand me.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 233
#6: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


I was under the impression that Platter meant...

Expand|Select|Wrap|Line Numbers
  1. <%System.Collections.ArrayList myList = (System.Collections.ArrayList)Session["horse"];%>
Platter? I've never put C# code on a web page before, so maybe you can't do this...
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


Yes Gary that is a valid way of doing it.
I think is a way to do the namespace part in the web page side as well, but i cannot remember the format
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 233
#8: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


I did a bit of googling... does this help at all?
http://msdn.microsoft.com/en-us/library/ms164642.aspx

It seems you need to put the namespace references in a web.config file somewhere. This looks like it talks about VB, but I found this same information in relation to C#... so maybe it works like this too?

As I said, I don't know much about this kind of thing, just trying to be helpful. It's definitely confusing... give me a compiler any day! ;)
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#9: Sep 29 '09

re: casting of object to retrieve individual value from arraylist


Are you positive its on the webpage part and not the code part?
I just threw this line in a page:
<%ArrayList mylist = new ArrayList();%>
And it accepted it just fine, no errors
Newbie
 
Join Date: Sep 2009
Posts: 6
#10: Sep 30 '09

re: casting of object to retrieve individual value from arraylist


Thanks guys im able to add <%ArrayList list2 = (ArrayList)Session["horse"];%> right now. It is after i put <add namespace="System.Collections"/> in my webconfig namespace.

But i have another problem now. My list2 in my <fb:profile-pic uid="<%=list2%>"
does not work. Any idea what should i put to get all the uid from the loop?
Expand|Select|Wrap|Line Numbers
  1. <%ArrayList list2 = (ArrayList)Session["horse"];%>
  2. <%for (int i = 0; i < 8; i++)%>
  3.  
  4.               <%{ %>
  5.  
  6.               <td> 
  7.  
  8.               <fb:profile-pic uid="<%=list2%>" facebook-logo="false" width="50" height="50"linked="true" />
  9.               </td>
  10.  
  11.               <%}%>
  12.  
Newbie
 
Join Date: Sep 2009
Posts: 6
#11: Sep 30 '09

re: casting of object to retrieve individual value from arraylist


Hi again, i did another alternative
Expand|Select|Wrap|Line Numbers
  1. <%ArrayList list2 = (ArrayList)Session["horse"];%> 
  2. <table cellpadding=0 cellspacing=11>
  3. <tr>
  4.               <td> 
  5.               <fb:profile-pic uid="<%=list2[0]%>" facebook-logo="true" width="50" height="50"linked="true" />
  6.               </td>
  7.               <td> 
  8.               <fb:profile-pic uid="<%=list2[1]%>" facebook-logo="true" width="50" height="50"linked="true" />
  9.               </td>
  10.               <td> 
  11.               <fb:profile-pic uid="<%=list2[2]%>" facebook-logo="true" width="50" height="50"linked="true" />
  12.               </td>
  13.               <td> 
  14.               <fb:profile-pic uid="<%=list2[3]%>" facebook-logo="true" width="50" height="50"linked="true" />
  15.               </td>
  16.               <td> 
  17.               <fb:profile-pic uid="<%=list2[4]%>" facebook-logo="true" width="50" height="50"linked="true" />
  18.               </td>
  19.  
  20. </tr>
  21. <tr>
  22.               <td> 
  23.               <fb:profile-pic uid="<%=list2[5]%>" facebook-logo="true" width="50" height="50"linked="true" />
  24.               </td>
  25.               <td> 
  26.               <fb:profile-pic uid="<%=list2[6]%>" facebook-logo="true" width="50" height="50"linked="true" />
  27.               </td>
  28.               <td> 
  29.               <fb:profile-pic uid="<%=list2[7]%>" facebook-logo="true" width="50" height="50"linked="true" />
  30.               </td>
  31.               <td> 
  32.               <fb:profile-pic uid="<%=list2[8]%>" facebook-logo="true" width="50" height="50"linked="true" />
  33.               </td>
  34.               <td> 
  35.               <fb:profile-pic uid="<%=list2[9]%>" facebook-logo="true" width="50" height="50"linked="true" />
  36.               </td>
  37.  
  38. </tr>
  39. </table>
  40.  
But it has an error saying Index was out of range. Must be non-negative and less than the size of the collection. I know in my sql there are only 6 uid, How do i make the array or arraylist size unlimited?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#12: Sep 30 '09

re: casting of object to retrieve individual value from arraylist


If there are only 6 entries, why would you think doing list2[9] would work?
Newbie
 
Join Date: Sep 2009
Posts: 6
#13: Sep 30 '09

re: casting of object to retrieve individual value from arraylist


Yea! i did it!
Now it works
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. </form> 
  4. <div> 
  5.     <table cellpadding=0 cellspacing=11> 
  6.     <tr> 
  7. <%ArrayList mylist = (ArrayList)Session["horse"];%>  
  8.         <%for (int i = 0; i < list2.Count; i++)%> 
  9.       <%{ %> 
  10.         <td>  
  11.         <fb:profile-pic uid="<%=mylist[i]>" facebook-logo="false" width="50" height="50"linked="true" /> 
  12.         </td> 
  13.       <%}%> 
  14.     </tr> 
  15.     </table> 
  16. </div> 
  17.  
  18.  
  19.  
haha, but it gives me one row only does anyone know how to split my pic

like

pic 1, pic 2, pic 3
pic 4, pic 5, pic 6
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#14: Sep 30 '09

re: casting of object to retrieve individual value from arraylist


Just start doing the math out?
Reply