Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Shared variable maintaining value

Status
Not open for further replies.

theoryofben

IS-IT--Management
Aug 25, 2001
149
US
I wasn't sure exactly how to phrase this but here goes. I have an asp.net page with vb.net for the code behind which does inserts on multiple tables. So I needed to generate a GUID that would act as a primary key on one table and foreign key on the others. I declared this as a Public Shared variable.

My understanding is that each time this page is loaded fresh a new id will be generated and available to all methods being used during that instance. This works fine on my development machine and two others that I test on. But on another machine, it doesn't.

I do an insert and then go do a second insert (after leaving the page) and the same GUID value is assigned which causes a primary key constraint error.

I realize I'm probably doing something royally stupid...but I need to know the answer. I just can't figure out why it works on some machines but not on others.

Code:
Public Class newentry
    Inherits System.Web.UI.Page

    Public leadid As Guid = System.Guid.NewGuid

    Dim currentuser As Guid = CType(Membership.GetUser(User.Identity.Name).ProviderUserKey, Guid)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
          
            bindDropdowns()
        End If

________________________________________________
[sub]"I have not failed. I've just found 10,000 ways that won't work."-Thomas Edison[/sub]

 
you're close. I had to do basically the same thing a while ago.

In your Page_Load, if not PostBack generate a new guid, otherwise, use the one on hand. You may want a button to explicitly clear this value if people are starting new "sessions" without closing the browser.

Lodlaiden

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
Thanks for the reply. I actually had to fix it quick and dirty. I removed the public variable and placed a standard variable in the page_load in if not postback then stuck it in a session variable. I guess that should cover it.

Any idea why the previous behavior is not the same on each server? Works fine on my development machine and a couple others. Weird.

________________________________________________
[sub]"I have not failed. I've just found 10,000 ways that won't work."-Thomas Edison[/sub]

 
Our test environments are perfect little worlds. We spin up a brand new IIS server each time we test, we run one or two functions, in our idyllic view of how the form is meant to be used and then shut the web server down.

Users do bad things, like keep windows open, and forget to cycle IIS in between uses. This is why there is no substitute for real user testing before going live with a product.

Lod

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top