如何检索包含对象或数组的会话. [英] How to retrive Session containing Object or array.

查看:53
本文介绍了如何检索包含对象或数组的会话.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebForm1.aspx.cs

//Store object to session.
object[] obj = new object[3];
obj[0] = 98.98; //Some value. Its Just Sample, to make things simple. Store required value.
obj[1] = (DataSet)ViewState["DataInfo"]; //We can store even DataSet.But Avoid storing DataSet instead store a key value from which u can retive ur dataSet again.
obj[2] = "xyz"; //Some value.
Session["obj"] = obj;


WebForm2.aspx.cs


WebForm2.aspx.cs

//Retrieve
object myobj2 = new object[3];
myobj2 = ((object[])Session["DataInfo"])[2];



//数组.
txtmyarray.Text =(string)((Array)Session ["myarray"])[0];

克里斯汀·格劳斯,
可能只是为了解决我的问题,我犯了错误...现在我已经纠正了...
根据您的建议,我已更正了我的上述代码.
感谢您的宝贵意见.我同意你的观点.



//Array.
txtmyarray.Text = (string)((Array)Session["myarray"])[0];

Christian Graus,
May be in simplyfing my problem, I have made mistakes... which now I have corrected...
I have corrected my above code, as per your suggestion.
Thanks for your valuable comments. I agree with you.

推荐答案

Minaxi Sajwan写道:
Minaxi Sajwan wrote:

myobj2 =(( object [])Session ["EpresInfo"])[2];

myobj2 = ((object[])Session["EpresInfo"])[2];



myobj2是一个对象数组,但是您正在尝试为其分配一个对象([2]这样做).您的整体方法非常糟糕.如果您必须在会话中存储不同的值,请分别存储它们(除非它们合在一起),然后将它们存储为结构.您的代码现在非常脆弱,您正在存储对象,而在另一端将假定这些对象是什么.如果有人决定在此处交换商品的订单怎么办?

另外,如果可以帮助的话,永远不要存储数据集,通常,存储可用于从数据库获取数据集的ID效率更高.



myobj2 is an array of objects, but you''re trying to assign a single object to it ( the [2] does this ). Your overall approach is very broken. If you have different values that you MUST store in the session, store them individually, unless they make sense together, then store them as a struct. Your code is VERY fragile right now, you''re storing objects, and on the other end will assume what those objects are. What if someone decides to swap the order of items here ?

Also, never store a DataSet if you can help it, it usually is more efficient to store an id which can be used to get the dataset from the database.


上面的代码是正确的.
由于存在问题,它没有给我答案."

首先,不要发布虚假答案,请编辑您的帖子以添加详细信息.

第二,捕捉问题"是什么意思?您是说它引发异常吗?那不是告诉你这是不正确的吗?


"Above my code is correct.
It is not giving me answer due to catching problem."

First of all, don''t post fake answers, edit your post to add details.

Second, what do you mean by ''catching problem'' ? Do you mean it throws an exception ? Doesn''t that tell you it''s not correct ?


Minaxi Sajwan写道:
Minaxi Sajwan wrote:

Session ["obj"] = obj;

Session["obj"] = obj;







Minaxi Sajwan写道:
Minaxi Sajwan wrote:

myobj2 =((object [])Session ["EpresInfo"])[2];

myobj2 = ((object[])Session["EpresInfo"])[2];



等一下.这两个会话值无关.如果您未在某处设置此值,则该值将爆炸,因为会话值将为null.是否正确(如果正确,则两个代码段无关,您的问题毫无意义),此代码为CRAP.就像我之前所说的那样,切勿在会话值中使用字符串代替它,这会导致各种错误.创建一个静态类来存储用于为会话值建立索引的字符串,这样就不会因为引号内的错字而出现怪异的错误.



Wait a minute. These two session values are unrelated. If you don''t set this value somewherer, this will blow up because the session value will be null. Correct or not ( and if it''s correct, the two snippets are unrelated and your question makes no sense ), this code is CRAP. As well as what I said before, NEVER use strings in place for Session values, it leads to all sorts of bugs. Create a static class to store the strings you use to index session values, so you never get weird bugs due to typos inside the quotes.


确定,这更有意义.我不明白为什么在印度,当他们进入某人,不付钱,不培训他们,并称我们为他们做到这一点时,他们就称其为见习生" .真丢脸.

您应该通读我的评论,该代码仍可操作,而不是生产质量.当然,您没有更改它,花了11分钟的时间(我等着您经过编辑后才能上床睡觉),只是添加了固定代码的注释和一个新的代码段.

OK, that makes more sense. I don''t get why in India, they call it a ''trainee'' when they get someone in, don''t pay them, and don''t train them, and hope we''ll do it for them. It''s disgraceful.

You should read through my comments, this code is still excrable, and not production quality. Of course, you''ve not changed it, you took 11 minutes ( I waited while you edited to help you before going to bed ), and just added the comment that you''d fixed the code, and a new code snippet.

Minaxi Sajwan写道:
Minaxi Sajwan wrote:

txtmyarray.Text =(string)((Array)Session ["myarray"])[0]

txtmyarray.Text = (string)((Array)Session["myarray"])[0]



这具有全新的会话值,与您之前发布的两个会话值无关.如果可行,则您发布的代码与之无关,因为第一个代码在数组的0索引中以不同的值存储了不是字符串的对象.

看来您正在庇护所工作,如果您阅读并按照我说的去做,可能要花几天时间才能成为比编写您正在工作的代码的小丑更好的程序员.上.或者,如果您自己编写了所有这些代码,我想您将会了解您编写的所有代码为何如此糟糕的原因,并学习如何编写类似的东西.



This has a whole new session value, unrelated to the two you posted before. If it works, then the code you posted is unrelated to it, as the first code stores an object that is not a string in the 0 index of the array, and under a different value.

It looks like you''re working in a sheltered workshop, if you read and work through the things I said, you''ll probably take a few days to be a better programmer than the clowns who wrote the code you''re working on. Or, if you wrote all this code yourself, i guess you''ll learn why all the code you wrote is terrible, and learn how to write something half decent.


这篇关于如何检索包含对象或数组的会话.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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