Question

This is really weird error i'm getting and i'll try and explain as best I can.

I have two pages - Page 1 (form) and Page 2 (completed page)

From page 1 I put a variable into a database and then do a server.transfer to page two like so...

Server.Transfer("Page2.aspx", True)

On page 2 I then grab the variable called paymentOnHold which is set on Page 1 and goes into the database...

Here is how I set paymentOnHold on Page 1

Public Property paymentOnHold() As String
    Get
        Dim _paymentOnHold As Object = ViewState("paymentOnHold")
        If _paymentOnHold IsNot Nothing Then
            Return CType(_paymentOnHold, String)
        Else
            Return Nothing
        End If
    End Get
    Set(ByVal value As String)
        If Not String.IsNullOrEmpty(value) Then
            ViewState("paymentOnHold") = value
        Else
            ViewState("paymentOnHold") = Nothing
        End If
    End Set
End Property

...

paymentOnHold = Date.Now.ToString("yyyyMMddHHmmss")

Here's how I grab the value on Page 2...

Dim myValue As String

 If TypeOf PreviousPage Is Page1 Then

        myValue = DirectCast(PreviousPage, Page1).paymentOnHold

End If

In my development environment where the databases are local the value in the DB and the value on page 2 both match - as you would expect...

In live environment the DB value is 3 or 4 seconds different (before) the one on Page 2 - even though I do not reset it or anything?

This has been driving me crazy for the last few hours and cannot work it.

Does anyone have any ideas/suggestions as to what might be causing this?

Thanks in advance

Was it helpful?

Solution

This could be an issue of saving the view state in first Page (form-1)

In asp.net Page lifecycle

  1. Initalization (controls raise their Init event)
  2. Load ViewState (Only on post back)
  3. Load PostbackData 
  4. Load
  5. Raise PostbackEvent
  6. Save View State
  7. Render

Server.Transfer() stops rendering the current page and starts rendering another one.That's why Server.Transfer() cannot be used to redirect to pages served by another server.

If you are doing Server.transfer before Event--> 6. Save View State you are not saving viewstate on the form-1

Solution

Response.redirect and session cache, as it is intended to exist per user and across multiple pages in the application.

OTHER TIPS

Using ViewState in this manner is a brittle solution, because ViewState is not intended to exist outside of the scope of the page it was initiated in, much less passed between pages, which I realize you are not quite doing, but you are getting dangerously close to doing it.

The better approach is to use Session cache, as it was intended to exist per user and span multiple page requests.

Try this:

To store in Session, do this:

Session("PaymentOnHold") = [Date].Now.ToString("yyyyMMddHHmmss")

To retrieve a value from Session, do this:

' First check to see if the value is in Session cache or not
If Session("PaymentOnHold") IsNot Nothing Then
    ' Everything in Session cache is stored as an object so you need to cast it to get it out
    Dim datePaymentOnHold As DateTime = TryCast(Session("PaymentOnHold"), DateTime)
End If

Now the Session value will be available no matter how you navigate to pages (Server.Transfer or Response.Redirect).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top