locked
How do I build comma delimited string of those values and stick them into a session variable. RRS feed

  • Question

  • User-858033455 posted

    I have this code that loop through table controls how do stick the seleted information into a 

    session variable. Then i have to pass it into another page table control.

    example code

    MyTableControl tbl = (MyTableControl)this.Page.FindControlRecursively("MyTableControl");
    if (tbl != null)
    {
    MyTableControlRecordControl[] rows = tbl.GetSelectedRecordControls();
    } 


    I appreciate those of you that will point to a link. But if you could give some sample code using the snippet above would be most helpful

    Friday, February 11, 2011 8:17 AM

Answers

  • User1844282159 posted

    I'm not sure which cell of the row you want in a comma separated string but to store the complete object in the session you can do it as below.

    To store "rows" in the session:

    MyTableControl tbl = (MyTableControl)this.Page.FindControlRecursively("MyTableControl");
    
    if (tbl != null)
    {
        MyTableControlRecordControl[] rows = tbl.GetSelectedRecordControls();
    }
    
    Session["MyTableControlRecordControls"] = rows;

    To retrieve again from the session:

    TextBox[] storedRows = Session["MyTableControlRecordControls"] as TextBox[];
    
    if (storedRows != null)
    {
        // Do your thing....
    }

    If you only want to store a comma separated list of values from a specific cell you can create a string adding each rows.cells[x] value to it.

    Without knowing the properties available in the "MyTableControl" or "MyTableControlRecordControl" objects you might need to change your code to fit in with the logic using your object properties.

    To store "myCommaSeparatedvalues" in Session:

    MyTableControl tbl = (MyTableControl)this.base.Page.FindControlRecursively("MyTableControl");
    if (tbl != null)
    {
        MyTableControlRecordControl[] rows = tbl.GetSelectedRecordControls();
    }
    
    string myCommaSeparatedvalues = string.Empty;
    
    foreach (MyTableControlRecordControl row in rows)
    {
        myCommaSeparatedvalues += 
            string.IsNullOrEmpty(myCommaSeparatedvalues) ? string.Empty : ",";
        myCommaSeparatedvalues += row.Cells[5];
    }
    
    Session["CommaSeparatedValues"] = myCommaSeparatedvalues;
    

    To retrieve again from the session:

    string myValues = Session["CommaSeparatedValues"] as string;
    
    if (!string.IsNullOrEmpty(myValues))
    {
        // Do your thing....
    }
    


    If I missunderstood your requirment please feel free to elaborate what exactly it is you need.

     

    Hope this helps.

    Kind Regards,

    Francois

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 11, 2011 9:28 AM
  • User1844282159 posted

    How do I write the Session part. I thought I knew but still getting Session = null;

     

    When you say you still getting Session = null, could you post your code you use to write to the Session object and the code you use to read from the Session Object?

    // To write
    Session["MySessionVariableName"] = MyObject;
    
    // The above stores your object in a session variable named "MySessionVariableName"
    
    // To read, use the same name you used to write to.
    MyObject = Session["MySessionVariableName"] as Type;

     

    Edit: This link might help with additional information on the session Object: Page.Session Property

    Kind Regards,

    Francois

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 12, 2011 4:34 AM

All replies

  • User-1360095595 posted

    You could stick the entire rows array into session, if that's what you intend.  And when reading it back, you can cast it back to the same array type.  I think that would work.

    Be aware that if you have a high volume website, putting too much in Session will eat up your server memory, especilly since you're doing this per user.

    Friday, February 11, 2011 9:07 AM
  • User1844282159 posted

    I'm not sure which cell of the row you want in a comma separated string but to store the complete object in the session you can do it as below.

    To store "rows" in the session:

    MyTableControl tbl = (MyTableControl)this.Page.FindControlRecursively("MyTableControl");
    
    if (tbl != null)
    {
        MyTableControlRecordControl[] rows = tbl.GetSelectedRecordControls();
    }
    
    Session["MyTableControlRecordControls"] = rows;

    To retrieve again from the session:

    TextBox[] storedRows = Session["MyTableControlRecordControls"] as TextBox[];
    
    if (storedRows != null)
    {
        // Do your thing....
    }

    If you only want to store a comma separated list of values from a specific cell you can create a string adding each rows.cells[x] value to it.

    Without knowing the properties available in the "MyTableControl" or "MyTableControlRecordControl" objects you might need to change your code to fit in with the logic using your object properties.

    To store "myCommaSeparatedvalues" in Session:

    MyTableControl tbl = (MyTableControl)this.base.Page.FindControlRecursively("MyTableControl");
    if (tbl != null)
    {
        MyTableControlRecordControl[] rows = tbl.GetSelectedRecordControls();
    }
    
    string myCommaSeparatedvalues = string.Empty;
    
    foreach (MyTableControlRecordControl row in rows)
    {
        myCommaSeparatedvalues += 
            string.IsNullOrEmpty(myCommaSeparatedvalues) ? string.Empty : ",";
        myCommaSeparatedvalues += row.Cells[5];
    }
    
    Session["CommaSeparatedValues"] = myCommaSeparatedvalues;
    

    To retrieve again from the session:

    string myValues = Session["CommaSeparatedValues"] as string;
    
    if (!string.IsNullOrEmpty(myValues))
    {
        // Do your thing....
    }
    


    If I missunderstood your requirment please feel free to elaborate what exactly it is you need.

     

    Hope this helps.

    Kind Regards,

    Francois

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 11, 2011 9:28 AM
  • User-858033455 posted

    How do I write the Session part. I thought I knew but still getting Session = null;

    Friday, February 11, 2011 9:57 PM
  • User1844282159 posted

    How do I write the Session part. I thought I knew but still getting Session = null;

     

    When you say you still getting Session = null, could you post your code you use to write to the Session object and the code you use to read from the Session Object?

    // To write
    Session["MySessionVariableName"] = MyObject;
    
    // The above stores your object in a session variable named "MySessionVariableName"
    
    // To read, use the same name you used to write to.
    MyObject = Session["MySessionVariableName"] as Type;

     

    Edit: This link might help with additional information on the session Object: Page.Session Property

    Kind Regards,

    Francois

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 12, 2011 4:34 AM
  • User-858033455 posted

    Is there a better way. Please advise for the furture. We have only 12 employees as of now. Later I can change it

    Saturday, February 12, 2011 9:41 AM
  • User-858033455 posted

    Thank you! here is my personal email Postonoh@att.net

    Maybe I can hire you from time to assist with code.


    fwahl

    Saturday, February 12, 2011 9:43 AM
  • User-858033455 posted

    fiqure out this portion with you help.


    I needed to add this this.Page.Session["MyTableControlRecordControlsl"] = rows; instead of Session[""] = rows;

    this fix my Session = null problem.

    Monday, February 14, 2011 4:57 PM