RosanneParnell
asked on
Saving an array in Session in C#
I have an array that I need to save and retrieve as a session variable.
I initialize and save the array as follows:
g_Move_Direction= new int[40];
for (i=0; i<40; i++)
{g_Move_Direction[i] =2;}
this.Session["Move_Directi on"]=g_Mov e_Directio n;
Later I retrieve the array as follows:
int[] g_Move_Direction = (int[]) this.Session["Move_Directi on"];
When I try and save a new value to the array using,
g_Move_Direction[g_segment _count]= Convert.ToInt32(this.radio _Direction .SelectedV alue);
I get the following error page: [Note I've verified that radio_Direction.SelectedVa lue is 1 when attempting to save the new value.]
************************** ********** ********** ********** ********** ********** ********** *****
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 411: segment_type=this.radio_Se gmentType. SelectedVa lue;
Line 412: //Save segment type, move direction & axis
Line 413: g_Move_Direction[g_segment _count]= Convert.ToInt32(this.radio _Direction .SelectedV alue);
Line 414: g_Segment_Type[g_segment_c ount]=segm ent_type;
Line 415: g_Segment_Axis[g_segment_c ount]=this .ddlAxialF orceDirect ion.Select edValue;
Source File: c:\vsprojects\manhattanapp s\actuator designerso lution\act uatordesig ner\comple x_move.asp x.cs Line: 413
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Actuator1.Complex_Move.btn SaveSegmen t_Click(Ob ject sender, EventArgs e) in c:\vsprojects\manhattanapp s\actuator designerso lution\act uatordesig ner\comple x_move.asp x.cs:413
System.Web.UI.WebControls. LinkButton .OnClick(E ventArgs e)
System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
System.Web.UI.Page.Process RequestMai n()
************************** ********** ********** ********** ********** ********** ********** *****
Am I saving/retrieving it correctly?
I initialize and save the array as follows:
g_Move_Direction= new int[40];
for (i=0; i<40; i++)
{g_Move_Direction[i] =2;}
this.Session["Move_Directi
Later I retrieve the array as follows:
int[] g_Move_Direction = (int[]) this.Session["Move_Directi
When I try and save a new value to the array using,
g_Move_Direction[g_segment
I get the following error page: [Note I've verified that radio_Direction.SelectedVa
**************************
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
Line 411: segment_type=this.radio_Se
Line 412: //Save segment type, move direction & axis
Line 413: g_Move_Direction[g_segment
Line 414: g_Segment_Type[g_segment_c
Line 415: g_Segment_Axis[g_segment_c
Source File: c:\vsprojects\manhattanapp
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Actuator1.Complex_Move.btn
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
**************************
Am I saving/retrieving it correctly?
ASKER
g_Move_Direction is of type int[40] and at this point has <undefined value> in the watch list
g_segment_count is of type int and has the value of 0 (zero).
g_segment_count is of type int and has the value of 0 (zero).
It seems that something happened with the g_Move_Direction meanwhile. Please check if you really got it back correct in line:
int[] g_Move_Direction = (int[]) this.Session["Move_Directi on"];
Another thing that pops into my mind is that you can have another variable declared in another scope and you use two different variables, no matter that they have same names?
int[] g_Move_Direction = (int[]) this.Session["Move_Directi
Another thing that pops into my mind is that you can have another variable declared in another scope and you use two different variables, no matter that they have same names?
ASKER
I'm new to .Net and C# so the scope is a bit confusing to me right now.
I have g_Move_Direction declared here:
namespace Actuator1
{
/// <summary>
/// Summary description for Complex_Move.
/// </summary>
public class Complex_Move : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls .HtmlForm Form1;
.
.
.
int[] g_Move_Direction;
and then in the Page_Load routine (for the first time through the page only - postback=false) I initialize it and save it:
g_Move_Direction= new int[40];
for (i=0; i<40; i++)
{g_Move_Direction[i] =2;}
this.Session["Move_Directi on"]=g_Mov e_Directio n;
on a post back to the page I retrieve it using:
int[] g_Move_Direction = (int[]) this.Session["Move_Directi on"];
Upon retrieving it (prior to attempting to store a value in it) I did confirm that the array is still set to all 2's.
I have g_Move_Direction declared here:
namespace Actuator1
{
/// <summary>
/// Summary description for Complex_Move.
/// </summary>
public class Complex_Move : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls
.
.
.
int[] g_Move_Direction;
and then in the Page_Load routine (for the first time through the page only - postback=false) I initialize it and save it:
g_Move_Direction= new int[40];
for (i=0; i<40; i++)
{g_Move_Direction[i] =2;}
this.Session["Move_Directi
on a post back to the page I retrieve it using:
int[] g_Move_Direction = (int[]) this.Session["Move_Directi
Upon retrieving it (prior to attempting to store a value in it) I did confirm that the array is still set to all 2's.
ASKER
Am I defining this variable (g_Move_Direction) in the wrong spot and then the code doesn't see it in the page functions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And if you would add another method:
public void Test3()
{
myVariable = 3;
Console.WriteLine(myVariab le);
}
and changed the Main:
public static void Main()
{
Test1(); // will write 1 (MyNamespace.MyClass.myVar iable)
Test2(); // will write 2 (MyNamespace.MyClass.Test2 .myVariabl e)
Test1(); // will write 1 (MyNamespace.MyClass.myVar iable)
Test3(); // will write 3 (MyNamespace.MyClass.myVar iable!!!, because no variable of the same name is declared)
Test1(); // will write 3 (MyNamespace.MyClass.myVar iable)
}
public void Test3()
{
myVariable = 3;
Console.WriteLine(myVariab
}
and changed the Main:
public static void Main()
{
Test1(); // will write 1 (MyNamespace.MyClass.myVar
Test2(); // will write 2 (MyNamespace.MyClass.Test2
Test1(); // will write 1 (MyNamespace.MyClass.myVar
Test3(); // will write 3 (MyNamespace.MyClass.myVar
Test1(); // will write 3 (MyNamespace.MyClass.myVar
}
ASKER
Thanks! That works!
g_Move_Direction,
g_segment_count
Tell me the exact values these have in line 413 in file "complex_move.aspx.cs" and tell me their exact types too.