To add Array in a Session

i have added an integer array to a session variable which will be in string, and i cant retrive it back, since there's a type cast mismatch. how to retrive it or how to type cast a string into an array back.

plz help me in this problem,

regards,
abul.




Comments

  • Why not just store the integer array rather than converting it to a string.

    Here is some sample code ...

    [code]
    Dim ia() As Integer = {1, 2, 3, 4}
    Session("ia") = ia

    Dim ib() As Integer = CType(Session("ia"), Integer())

    For Each i As Integer In ib
    Response.Write(i & "
    ")
    Next
    [/code]

    If u have to go the long route, then you will have to manually do it ...

    [code]
    Session("ia") = "1,2,3,4"

    Dim tmp As String = Session("ia").ToString()
    Dim sb() As String = tmp.Split(","c)
    Dim ib(sb.GetUpperBound(0)) As Integer

    For i As Integer = 0 To sb.GetUpperBound(0)
    ib(i) = Integer.Parse(sb(i))
    Next

    For Each j As Integer In ib
    Response.Write(j & "
    ")
    Next
    [/code]


    : i have added an integer array to a session variable which will be in string, and i cant retrive it back, since there's a type cast mismatch. how to retrive it or how to type cast a string into an array back.
    :
    : plz help me in this problem,
    :
    : regards,
    : abul.
    :
    :
    :
    :
    :

  • thanks for replying me, still i have doubts. spare just few minutes.....
    how to get the unknown no of variables. my code will be similiar to this,

    L1: int id[] = new int[50]; // defining the integer array

    L2:for(i=0;i<n;i++) // n<50, n=total no of variables.
    L3:id[i]={1,3,5,.....n}; // getting 'n' number of variables from any source, and in my case datareader(from sql db) and adding into the array.

    L4: session("userid") = id; // adding array to session
    And from this, how to get the same integer array from session.
    i tried,

    L5: int[] GetId = new int[50];
    L6: GetId = session("userid").ToString(); // this is how is tried to get back, but Error in this line.
    L7: GetId = Convert.ToInt32(session("userid").ToString());// Error in this line also.

    please help me solving this in C#, or give me a reference link.
    thank you for your interest.
    regards,
    abul.


    : Why not just store the integer array rather than converting it to a string.
    :
    : Here is some sample code ...
    :
    : [code]
    : Dim ia() As Integer = {1, 2, 3, 4}
    : Session("ia") = ia
    :
    : Dim ib() As Integer = CType(Session("ia"), Integer())
    :
    : For Each i As Integer In ib
    : Response.Write(i & "")
    : Next
    : [/code]
    :
    : If u have to go the long route, then you will have to manually do it ...
    :
    : [code]
    : Session("ia") = "1,2,3,4"
    :
    : Dim tmp As String = Session("ia").ToString()
    : Dim sb() As String = tmp.Split(","c)
    : Dim ib(sb.GetUpperBound(0)) As Integer
    :
    : For i As Integer = 0 To sb.GetUpperBound(0)
    : ib(i) = Integer.Parse(sb(i))
    : Next
    :
    : For Each j As Integer In ib
    : Response.Write(j & "")
    : Next
    : [/code]
    :
    :
    : : i have added an integer array to a session variable which will be in string, and i cant retrive it back, since there's a type cast mismatch. how to retrive it or how to type cast a string into an array back.
    : :
    : : plz help me in this problem,
    : :
    : : regards,
    : : abul.
    : :
    : :
    : :
    : :
    : :
    :
    :

  • Here is a C# example that will answer your doubts ...

    [code]
    int[] id = new int[10];

    for(int i=0; i < id.Length; i++)
    id[i]=i;

    Session["userid"] = id;
    [b]int[] GetId = (int[])Session["userid"];[/b]

    for(int j=0; j < GetId.Length; ++j)
    Response.Write(GetId[j] + " ");
    [/code]

    The bold line is what should interest you. Basically, I define the GetId array (with an unknown bounds) and assign the contents of the Session by casting. The part that does the casting is:

    [code]
    (int[])
    [/code]

    This code states that we know the contents of the Session, at this point, contains an integer array. If the array in the session has 10 elements then GetId will have 10 elements and so on ...

    : thanks for replying me, still i have doubts. spare just few minutes.....
    : how to get the unknown no of variables. my code will be similiar to this,
    :
    : L1: int id[] = new int[50]; // defining the integer array
    :
    : L2:for(i=0;i<n;i++) // n<50, n=total no of variables.
    : L3:id[i]={1,3,5,.....n}; // getting 'n' number of variables from any source, and in my case datareader(from sql db) and adding into the array.
    :
    : L4: session("userid") = id; // adding array to session
    : And from this, how to get the same integer array from session.
    : i tried,
    :
    : L5: int[] GetId = new int[50];
    : L6: GetId = session("userid").ToString(); // this is how is tried to get back, but Error in this line.
    : L7: GetId = Convert.ToInt32(session("userid").ToString());// Error in this line also.
    :
    : please help me solving this in C#, or give me a reference link.
    : thank you for your interest.
    : regards,
    : abul.
    :
    :
    : : Why not just store the integer array rather than converting it to a string.
    : :
    : : Here is some sample code ...
    : :
    : : [code]
    : : Dim ia() As Integer = {1, 2, 3, 4}
    : : Session("ia") = ia
    : :
    : : Dim ib() As Integer = CType(Session("ia"), Integer())
    : :
    : : For Each i As Integer In ib
    : : Response.Write(i & "")
    : : Next
    : : [/code]
    : :
    : : If u have to go the long route, then you will have to manually do it ...
    : :
    : : [code]
    : : Session("ia") = "1,2,3,4"
    : :
    : : Dim tmp As String = Session("ia").ToString()
    : : Dim sb() As String = tmp.Split(","c)
    : : Dim ib(sb.GetUpperBound(0)) As Integer
    : :
    : : For i As Integer = 0 To sb.GetUpperBound(0)
    : : ib(i) = Integer.Parse(sb(i))
    : : Next
    : :
    : : For Each j As Integer In ib
    : : Response.Write(j & "")
    : : Next
    : : [/code]
    : :
    : :
    : : : i have added an integer array to a session variable which will be in string, and i cant retrive it back, since there's a type cast mismatch. how to retrive it or how to type cast a string into an array back.
    : : :
    : : : plz help me in this problem,
    : : :
    : : : regards,
    : : : abul.
    : : :
    : : :
    : : :
    : : :
    : : :
    : :
    : :
    :
    :

  • hey thanks a lot, its working...
    u r doing a great work in this site. keep helping us..
    thanks......

    : Here is a C# example that will answer your doubts ...
    :
    : [code]
    : int[] id = new int[10];
    :
    : for(int i=0; i < id.Length; i++)
    : id[i]=i;
    :
    : Session["userid"] = id;
    : [b]int[] GetId = (int[])Session["userid"];[/b]
    :
    : for(int j=0; j < GetId.Length; ++j)
    : Response.Write(GetId[j] + " ");
    : [/code]
    :
    : The bold line is what should interest you. Basically, I define the GetId array (with an unknown bounds) and assign the contents of the Session by casting. The part that does the casting is:
    :
    : [code]
    : (int[])
    : [/code]
    :
    : This code states that we know the contents of the Session, at this point, contains an integer array. If the array in the session has 10 elements then GetId will have 10 elements and so on ...
    :
    : : thanks for replying me, still i have doubts. spare just few minutes.....
    : : how to get the unknown no of variables. my code will be similiar to this,
    : :
    : : L1: int id[] = new int[50]; // defining the integer array
    : :
    : : L2:for(i=0;i<n;i++) // n<50, n=total no of variables.
    : : L3:id[i]={1,3,5,.....n}; // getting 'n' number of variables from any source, and in my case datareader(from sql db) and adding into the array.
    : :
    : : L4: session("userid") = id; // adding array to session
    : : And from this, how to get the same integer array from session.
    : : i tried,
    : :
    : : L5: int[] GetId = new int[50];
    : : L6: GetId = session("userid").ToString(); // this is how is tried to get back, but Error in this line.
    : : L7: GetId = Convert.ToInt32(session("userid").ToString());// Error in this line also.
    : :
    : : please help me solving this in C#, or give me a reference link.
    : : thank you for your interest.
    : : regards,
    : : abul.
    : :
    : :
    : : : Why not just store the integer array rather than converting it to a string.
    : : :
    : : : Here is some sample code ...
    : : :
    : : : [code]
    : : : Dim ia() As Integer = {1, 2, 3, 4}
    : : : Session("ia") = ia
    : : :
    : : : Dim ib() As Integer = CType(Session("ia"), Integer())
    : : :
    : : : For Each i As Integer In ib
    : : : Response.Write(i & "")
    : : : Next
    : : : [/code]
    : : :
    : : : If u have to go the long route, then you will have to manually do it ...
    : : :
    : : : [code]
    : : : Session("ia") = "1,2,3,4"
    : : :
    : : : Dim tmp As String = Session("ia").ToString()
    : : : Dim sb() As String = tmp.Split(","c)
    : : : Dim ib(sb.GetUpperBound(0)) As Integer
    : : :
    : : : For i As Integer = 0 To sb.GetUpperBound(0)
    : : : ib(i) = Integer.Parse(sb(i))
    : : : Next
    : : :
    : : : For Each j As Integer In ib
    : : : Response.Write(j & "")
    : : : Next
    : : : [/code]
    : : :
    : : :
    : : : : i have added an integer array to a session variable which will be in string, and i cant retrive it back, since there's a type cast mismatch. how to retrive it or how to type cast a string into an array back.
    : : : :
    : : : : plz help me in this problem,
    : : : :
    : : : : regards,
    : : : : abul.
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories