简体   繁体   中英

maintaining a variable in c# through different ASP sessions

Hello I have a Session ID variable I'm passing through from on Oracle package into an asp page. What I want to do is hold on to that variable and pass it back to Oracle through a different call. I would think this should be easy but I lose the variable value.

Everything else in the solution is working perfectly. It's just a small addition.

I'm not too sure how much information is too much or too little on this question but basically I have a connection class with different methods to call different procs in Oracle. Each one is called by pressing one of two buttons on the asp page.

One method (on button click) will pass in the Session ID (along with cursor results that exist in a permanent Oracle table).

The asp page then displays the cursor results.

The second method (on button click) then calls a proc in Oracle to pass whatever exists in that table to another Table based on the Session ID.

In order to ensure the right contents are passed I need the asp page to hold on to the session id (I got this far) then pass it back to Oracle on a different call.

in the declaration we have

private string outSessionID;

    public string OutSessionID
    {
        get { return outSessionID; }
        set { outSessionID = value; }
    }

section of the connection method that sets the parameter

addDbParameter(ref cmd, "outSession_ID", DbType.String, ParameterDirection.Output, null);

addDbParameter(ref cmd, "rc1", DbType.Object, ParameterDirection.Output, null);

 outSessionID = cmd.OutParameter<String>("outSession_ID");

in second method we have

addDbParameter(ref cmd, "inSession_ID", DbType.String, ParameterDirection.Input, OutSessionID);

I'm new to ASP so any insight on why this information is lost is appreciated. I think it has something to do with a postback being a new session or something but I'm not precisely sure.

I"ll be happy to extrapolate with more code but don't want to provide anything unnecessary.

Thanks.

Just to add that in the end I added a small method that collected the sessionId in the aspx.cs then called that method after ther postback. All the troubles occurred when trying to set the value within the pre-init to page_load methods. By calling a separate method after the click events I was able to save and recall the sessionid successfully.

Thanks for the responses, they were very helpful.

If that information is not security-prone, one approach is you can have a hidden field on your asp.net page. NOTE: the user can view the source of your page and see the content of the hidden field

<as:HiddenField runat="server" ID="MySessionID" />

Then after getting your sessionId you can store it

MySessionID.Value = outSessionID;

and to use it later

outSessionID = MySessionID.Value;

Here is another trick

public string OutSessionID
{
    get { return ViewState["outSessionID"].ToString(); }
    set { ViewState["outSessionID"] = value; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM