Where to store currently selected item id? - asp.net

I'm trying to write an asp.net app which searches a sql db and returns/updates the data.
I want to be able to hold the currently selected id of the item my user has selected. For example a doctors surgery would select a patient record then be able to browse the app without having to re-select that patient on each page.
What would be the best way to do this. Ideally I need to be able to get this ID application wide. the only thing i can think of is to create a public class, store the id and make it public but this seems quite messy
Thank you

Store it in a Cookie or a Session variable.
Cookie info
http://msdn.microsoft.com/en-us/library/ms178194.aspx
Session Info
http://msdn.microsoft.com/en-us/library/ms972429.aspx

I would recommend storing it in a session variable:
Page.Session["CurrentPatient"] = YourPatient record
To get the record you would use:
YourPatientRecord = Page.Session["CurrentPatient"] as PatientRecord;
To make things easier I usually create a property in the page or base page to use throughout the system.
eg:
protected PatientRecord CurrentPatient
{
get
{
return Session["CurrentPatient"] as PatientRecord;
}
set
{
Session["CurrentPatient"] = value;
}
}
Then to use it in the page it would simply be:
PatientRecord oPatientRecord = this.CurrentPatient;

Related

MVC - how can I store a variable for the duration that a user is logged in

I have a project in MVC. I would like to save a variable and have it accessible as long as the user is logged in, to get it or set it.
The reason for this is that the application uses the information I would put in there, to get data. I now need to add admin functions so an admin can see more then only his own results, and therefore I would need to change this ID, depending on what result he wants to see.
I have tried using a session, but the problem I have with that is that when the user closes the website, and at a later time returns, he is still logged in, but the session variable is null.
I also tried to add a property to my base class. I was able to set it, but when I tried to get it in a different controller, the property was null as well.
What is the best/fastest/correct way to do this? I would prefer to not use the database for this, if possible.
What you are looking for is a "Session". On login you can just do
Session["Key"] = "Value";
And on logout you can empty the field
Session["Key"] = String.Empty;
You can read more about sessions here: https://msdn.microsoft.com/en-us/library/ms178581.aspx

Share data between aspx pages

I need to share data (string, list, array) between two different aspx pages of the same application. What is the best way to do it if I do not want to use cookies and do not want for data to be visible in url.
a) Form post method
b) Session (cookies?)
c) Sql
d) Server.Transfer
Thanks
In-memory Session will be the simplest and quickest (development-wise) to store data between pages without their contents being visible in the query string (URL), like this:
To store a List<string> in Session, do this:
var listOfStrings = new List<string>();
listOfStrings.Add("1");
listOfStrings.Add("2");
listOfStrings.Add("3");
Session["ListOfStrings"] = listOfStrings;
To retrieve the List<string> from Session, do this:
// Check to see if item in Session is actually there or not
if(Session["ListOfStrings"] != null)
{
// Cast the item in Session to a List<T>, because everything in Session is an object
var myListOfStringsRetrieved = Session["ListOfStrings"] as List<string>;
}
Note: I am assuming you use C#, but this can easily be translated to VB.NET.
Some more details might be helpful. What type of information do you want to share? If it is something that needs to be saved, then perhaps it makes most sense to save the data in your database (or local storage, or what ever you are using) from one page and retrieve it in the other. If it's just temporary data, it probably makes more sense to post the data through a form, or use a session variable. The problem with the session variable is that you might time-out your session. A session variable wouldn't be my first choice.

Session ASP.NET

Session question in asp.net/c#
I have multiple asp.net pages and a class object.
e.g.
Class User object contains
public string Name { set; get; }
public string Address { set; get; }
etc.....
pg1.aspx: when button is clicked, the data is stored in the session and redirects to pg2.aspx
e.g. pg1.aspx
User u = new User();
u.Name = TextBox1.Text;
Session.Add("USERINFO", u);
Response.Redirect("pg2.aspx");
e.g. pg2.aspx //reading the data..
User u = ((User)Session["USERINFO"]);
Response.Write(u.Name + "<br/>");
..etc.. ( I have 5 pages and the data is passed around from page to page in a session) once the user reaches the last page it stores in the database otherwise I dont want to store incomplete data in the database.
It all works fine except if I use multiple tabs to run same web app, the data overwrites eachother... It works fine if I use separate browsers, but not with tabs...
How can I avoid a user to enter the data in tab1 get to page 3 and if the user opens tab2 and continues not to over the data was entered in tab1....
I hope it makes sense... what I am trying to accomplish... Thank you.
This is probably a duplicate question, so I'll simply redirect you to some answers that would solve your problem.
asp.net - session - multiple browser tabs - different sessions?
Unfortunately, the accepted answer won't work since it maintains a "unique" session identifier for each page, not your group of 5 pages.
Your browser version and the different methods of launching new tabs and windows in it determines whether sessions are shared across multiple tabs and windows. Controlling it could be done with
The modification to web.config as described in the linked article
Coming up with a unique session key prefix for each session value
Using some logic to prevent the same user from opening multiple sessions on the same machine
The simplest way to work around your current implementation would likely be to check whether or not you have the information you would expect from that page, and if the conditions aren't met then require the data to be input, or redirect appropriately depending on which stage in the process you are (based on the data that is available).
So, as well as adding to the session, you can get:
var value = Session[key] as string;
if (value != null)
{
//move on to the next appropriate page
//after determining which that is by other conditions
}
//we obviously need the page at which we get this data!
And you can build upon your conditions from there

How to forward data from one page to another

I want to take my data tables data to another page. How can I do this? I am programming in ASP.NET.
You can use QueryString to pass data.
If the data is the same on all pages use the Cache. Otherwise retrieve the data again from the database.
Also you can take a look at this article about possible ways of state management in ASP.NET: Nine Options for Managing Persistent User State in Your ASP.NET Application.
You can pass it as a session variable:
Session['yourData'] = dataYouWantToPass;
Then in the next page you retrieve it like so:
var dataYouWantToPass = (YourDataType)Session['yourData'];
In case of string data it would look like this:
//store in session
Session['stringData'] = "Test string data";
//read from session
var stringData= (String)Session['stringData'];

Best way to store commonly used values

In my Webforms 3.5 application, I have a GridView of users that displays the last time they logged in. This value is stored in the Users table in UTC. When an Administrator views the grid, I want them to view it the time zone the Administrator has selected in their preferences (also stored in the Users table).
I have this working properly for the GridView:
<ItemTemplate>
<%# TimeZoneInfo.ConvertTimeFromUtc(Eval("LastLoginDateTimeUTC"), TimeZoneInfo.FindSystemTimeZoneById(UserService.GetUser(HttpContext.Current.User.Identity.Name).DisplayTimeZone))%>
</ItemTemplate>
So in this case, every time a row is created, a user object is created.
What I would like to do is determine the best way to handle returning commonly used User specific data (such as user.DisplayTimeZone). I have several ideas in mind, but I wanted to get some input on how others are doing it. I was looking at using a User Specific Cache, but didn't want to implement until I had more ideas.
I would like to avoid the Session object, but it's not a requirement. I would prefer to use the HttpRuntime.Cache.
Also, once this is determined, where should the code for this go? In a BasePage class? In the MasterPage? In an MVP BasePresenter?
Thanks
~S
I've done the same using a user specific cache as you mentioned. I actually implemented it in a separate namespace though as static get properties. For example:
namespace MyWeb.Session
public static class CurrentUser {
public static int DisplayTimeZone {
get {
// Check cache first.
...
// Cache miss, load from database and store in cache.
...
}
}
}
}
That way every time I needed the value I simply call MyWeb.Session.CurrentUser.DisplayTimeZone.

Resources