Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
WebForm1.aspx.cs
C#
//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
C#
//Retrieve
object myobj2 = new object[3];
myobj2 = ((object[])Session["DataInfo"])[2];


//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.
Posted
Updated 14-Feb-10 23:31pm
v6

Minaxi Sajwan wrote:
myobj2 = ((object[])Session["EpresInfo"])[2];


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.
 
Share this answer
 
"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 wrote:
Session["obj"] = obj;



Minaxi Sajwan wrote:
myobj2 = ((object[])Session["EpresInfo"])[2];


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.
 
Share this answer
 
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 wrote:
txtmyarray.Text = (string)((Array)Session["myarray"])[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.
 
Share this answer
 

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