I am using visual basic dot net, with the asp.net add on. I have created a programme which works great, basically it has a logon, which uses mysql for the data, then loads a 2nd page using the username data. I am using the global variable to store the username between pages. Its all good, untill another person logs in at the same time as the first user, now when they log on, user 1 now has the globale variable of the 2nd user.
What I need is a globale variable that is unique to each user. Any idea how this is done, I am very new to asp.net.

Dani AI

Generated

— the problem you described happens when the username is stored in an application-wide variable (a Shared/static field or the Application collection). Those are global to the whole app domain, so every request sees the same value and one login can overwrite another. was right to point toward cookies/sessions: the usual, simple fix in ASP.NET is to store per-user data in Session (or use the authentication system), not in a global variable.

Example (VB.NET, WebForms): store the username on successful login, then read it on other pages.

' On login (after validating credentials)
Session("UserId") = userId
Session("UserName") = username
Response.Redirect("SecondPage.aspx")
' On another page (Page_Load)
Dim username As String = If(Session("UserName") IsNot Nothing, CStr(Session("UserName")), "")

Notes and cautions: do NOT use Application("...") or a Shared/Static variable to hold per-user state — 's "dynamic array" approach would still live in server memory and be shared across users. By default ASP.NET ties Session to a session cookie (ASP.NET_SessionId); you generally only store a small identifier (user id) in Session and query the database for details as needed. For production/web-farm setups, the default InProc session is fragile (lost on app recycle and not shared across nodes) — switch to StateServer, SQLServer, or a distributed cache, or use cookie/JWT-based auth (encrypt sensitive data).

Quick troubleshooting: if Session("UserName") is Nothing, check that the browser accepts cookies (or you configured cookieless sessions), verify session timeout in web.config, and reproduce using separate browsers or private windows to simulate concurrent users.

Recommended Answers

All 4 Replies

I am not familiar with .net, but typically the way this is done across all web development languages is with the use of cookies. A cookie is stored in a user's web browser indicating a session ID. That session ID is then stored in your server and maps to what user ID that user is logged in as.

Ah, yes of course, hadn;t thought of cookies, I will look up how to use those, thanks for the advice.
Geoff.

Have you figured out how to do .net sessions with cookies? Sorry, I don’t have .net experience to be able to provide you with actual code examples.

If you’re still stuck, I can try to research a bit.

You can use Dynamic array to store your users.

commented: Very old post, user probably found a solution by now. :) -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.