Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all I am trying to retrieve to session to an int array, but I am getting an error as Unable to cast object of type 'System.Int32' to type 'System.Int32[]'.

This is what I did

C#
int[] year = (int[])Session["year"]; // For this I am getting that error
DateTime[] dtStartPayPeriodDate = (DateTime[])Session["StartDates"];
DateTime[] dtEndPayPeriodDate = (DateTime[])Session["EndDates"];

for the remaining it works fine can any one tell what's wrong
Posted
Updated 25-Apr-12 3:27am
v2
Comments
woopsydoozy 25-Apr-12 9:27am    
Can you show us how you're putting the value INTO session?
demouser743 25-Apr-12 9:35am    
This is how I assigned, intitally declared private int[] iPayrollYear; through code I will assign values to the variable then the sessiion is as below Session["year"] = iPayrollYear;

This is telling you that you only have a single int value in Session. What you need to do is retrieve the value and assign it separately. A simple implementation would look like this:
C#
int[] year = new int[] { (int)Session["year"] };
This isn't something I would normally do, but it should work for you.
 
Share this answer
 
Comments
demouser743 25-Apr-12 10:13am    
Getting error as Object reference not set to an instance of an object. If I write as per said

int[] year = new int[] { (int)Session["year"] };
Pete O'Hanlon 25-Apr-12 10:23am    
If Session["year"] doesn't return anything, it will be null, so you won't be able to cast it. I've just tried this code and it's working fine for me, so I suspect you need to check to see what's in Session["year"].
demouser743 25-Apr-12 10:26am    
My session is as follows

http://i.stack.imgur.com/lXTmz.png
Pete O'Hanlon 25-Apr-12 10:40am    
There's nothing in there to show the value of Session["year"]. Session is a static class, which exposes an indexer through which you can retrieve values from it, i.. Session[0] or Session["year"].
The session contains the variable of type 'int' and you are trying to cast it to 'int []'. So it will not work.

The corrected code is as follows:

C#
int year = Convert.ToInt32(Session["year"].ToString()); // corrected
 
Share this answer
 
v2
Are you sure the value stored in
C#
Session["year"]
is an array of Int?

It looks like all you have in that variable is a single integer.

Try this:
C#
int year = int.Parse(Session["year"]);
 
Share this answer
 
Comments
demouser743 25-Apr-12 10:17am    
Yeah it is an int array
I think you are doing right type casting...

check your code by my below code

C#
int[] my=new int[5];
           my[0]=1;
           my[1]=2;
           my[2]=3;
           my[3]=6;
           my[4]=9; 
           Session["year"] = my;

           int[] year = (int[])Session["year"];



check before doing casting session is null or not and contain right integer array.

thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900