Solved

Cant delete a cookie immediatly during execution PostBack - VB.NET

Posted on 2006-11-28
8
567 Views
Last Modified: 2008-02-01
Hi,

I need to delete/expire a cookie in realtime while processing code. This is for when I empty my shopping cart, I want the cookie gone before the cart (which is ascx web control) processes as part of the normal postback procedure.

Can anyone tell me why this code doesn't immediatly expire the cookie?

        ' Set a cookie
        System.Web.HttpContext.Current.Response.Cookies("GUID").Value = "HelloWorld"

        ' expire cookie
        Dim aCookie As HttpCookie = Request.Cookies("GUID")
        aCookie.Expires = DateTime.Now.AddDays(-1)
        Response.Cookies.Add(aCookie)

        ' see if we can still read the cookie, WE CAN!!! Not what we want!  
         Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value

After the postback event has completed the cookie is expired, but I really need the cookie expired before I finish executing my postback code!

I know I can do something like Response.Redirect(Request.Url.ToString) at the end of all my code to force 'another' refresh of my page to a pre postback state, but this means about 10 trips back to the database etc.
0
Comment
Question by:bhermer
8 Comments
 
LVL 5

Expert Comment

by:prajapati84
ID: 18034686
Just try this one, u will find what u want...

Label1.Text = System.Web.HttpContext.Current.Response.Cookies("GUID").Value

Regards,
Mukesh
0
 
LVL 6

Expert Comment

by:badalpatel
ID: 18036479
just add this line before ur line of code..

Response.Cookies("GUID").Expires = DateTime.Now()
Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18036873
Hmmm, you are getting a cookie from the Request, setting the expiration, and then adding it to the Response, and the Request still has the cookie.  What is supposed to remove it from the Request object?

Bob
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 4

Author Comment

by:bhermer
ID: 18037629
Hey Bob, not sure I know what I am doing at all!!,

essentially I have a cookie which carries a GUID. I have a web component that sits on the page acting as a cart monitor, this will read the cookie (it reads a Session Var first and then reads the cookie if not there) and extracts the cart from the DB. Once the customer has paid, the cookie gets deleted and the session var gets reset. The problem being, due to the nature of web components, after I delete the cookie and before the page has finished executing the cart monitor re-renders, it looks at the cookie, which hasn't expired until the page finishs loading, finds it, resets the bl**dy session variable and shows as having items in the cart!

After the page is rendered, and refreshed, the cookie is gone. it is just during code execution that the cookie doesnt seem to be deleted.

badalpatel, that doesnt work, and I am pretty sure that is the same as expiring it one day in the past?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 18037752
Looking at the cart monitor component, can it check the expiration date on the cookie?

Bob
0
 
LVL 5

Expert Comment

by:prajapati84
ID: 18042930
You need to postback once to see the effect of cookie after its expiration. I think u should store value of cookie in any contral (e.g. textbox) and then at the time of expiration of cookie, make the value of that control as blank.

Regards,
Mukesh
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question