谁能告诉我如何将Session分配给int [] [英] Can any one tell how can I assign Session to int[]

查看:81
本文介绍了谁能告诉我如何将Session分配给int []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试检索到与int数组的会话,但是由于无法将类型为"System.Int32"的对象转换为类型为"System.Int32 []",因此出现错误'.

这就是我做的

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

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

推荐答案

这是在告诉您在Session中只有一个int值.您需要做的是获取值并分别分配.一个简单的实现应如下所示:
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:
int[] year = new int[] { (int)Session["year"] };

这不是我通常会做的事情,但它应该对您有用.

This isn''t something I would normally do, but it should work for you.


该会话包含类型为"int"的变量,您正尝试将其强制转换为"int []".因此它将不起作用.

更正后的代码如下:

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:

int year = Convert.ToInt32(Session["year"].ToString()); // corrected


您确定
Session["year"]

中存储的值是整数数组?

看起来该变量中的所有内容都是一个整数.

尝试以下操作:

is an array of Int?

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

Try this:

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


这篇关于谁能告诉我如何将Session分配给int []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆