Cookie in asp.net VB

Hi

I am trying to save a language preference on a website using cookies in VB asp.net.  There are 3 options, when the user opens the webpage it should check for a cookie. If there is none, then the page lets the user set up the language preference let’s say “English”. So when the user closes the page and they open it again,  the page will check for the user language cookie and display it, i.e. “English” to them.  This will be the case till the user clears the preference or when the cookie expires. I want to set the cookie to expire the next working day, at least for now.

But with this code and webpage that I have written, I always get the message that the cookie has expired. I don’t know what I am doing wrong

Thank you



Partial Class cookie
    Inherits System.Web.UI.Page

    Private lang As String
    Private cookiename As String = "UserSettings"

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        lang = Request.Form("Language")
        If Not Page.IsPostBack Then get_cookie()
        If Page.IsPostBack Then make_cookie()

    End Sub

    Private Sub make_cookie()
        Dim oldCookie As HttpCookie = Request.Cookies(cookiename)

        If oldCookie Is Nothing Then                'If there is no cookie then we will make one
            Dim newCookie As HttpCookie = New HttpCookie(cookiename)
            newCookie("Language") = lang
            newCookie.Expires = DateTime.Now.AddDays(1)
            Response.Cookies.Add(newCookie)
            Response.Write("Cookie was saved to remember your Preference<br />" & lang)
            Label1.Text = lang
        ElseIf oldCookie IsNot Nothing Then         'If there was a cookie then we will update it
            Dim nCookie As HttpCookie
            nCookie = New HttpCookie(cookiename)
            nCookie.Expires = DateTime.Now.AddDays(1)
            nCookie("Language") = lang
            Response.Cookies.Add(nCookie)
            Response.Write("Cookie was Modified<br />")
            Label1.Text = lang
        End If

    End Sub

    Private Sub get_cookie()

        Dim saved_lang As String
        Dim oldCookie As HttpCookie = Request.Cookies(cookiename)

        If oldCookie Is Nothing Then                            ' no cookie was found
            Response.Write("There is no Cookie <br/>")
        ElseIf (oldCookie.Expires > DateTime.Now) Then          ' cookie is valid
            saved_lang = oldCookie.Values("Language").ToString()
            Label1.Text = saved_lang
            If saved_lang = "English" Then
                English.Checked = True
            ElseIf saved_lang = "French" Then
                French.Checked = True
            Else
                None.Checked = True
            End If

            Response.Write("<br /><br />")

        ElseIf oldCookie.Expires < DateTime.Now Then            ' no cookie is expired
            Response.Write("Cookie has Expired <br/>")
            Label1.Text = "None"
        Else
            Response.Write("?")                                 ' some other state
            Label1.Text = "Unavailable"
        End If

    End Sub

    Protected Sub clear_cookie()

        Dim oldCookie As HttpCookie = Request.Cookies(cookiename)

        If (oldCookie IsNot Nothing) Then

            Dim myCookie As HttpCookie
            myCookie = New HttpCookie(cookiename)
            myCookie.Expires = DateTime.Now.AddHours(-6)
            Response.Cookies.Add(myCookie)
            French.Checked = False
            English.Checked = False
            None.Checked = True
            Label1.Text = "None"
        End If

    End Sub

    Protected Sub Button_Clear_Click(sender As Object, e As System.EventArgs) Handles Button_Clear.Click

        clear_cookie()
    End Sub

End Class

Open in new window





<%@ Page Language="VB" AutoEventWireup="false" CodeFile="cookie.aspx.vb" Inherits="cookie" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Cookie</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hi, 
        your saved language preference is : 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    <br />
    <asp:RadioButton ID="English" runat="server" GroupName="Language" 
        Text="English" />
    <br />
    <br />
    <asp:RadioButton ID="French" runat="server" GroupName="Language" 
        Text="French" />
    <br />
    <br />
    <asp:RadioButton ID="None" runat="server" GroupName="Language" 
        Text="None" />
    <br />
    <br />
    <asp:Button ID="Button_Save" runat="server" Text="Save Preference Cookie" />
&nbsp;<asp:Button ID="Button_Clear" runat="server" Text="Clear Cookie" />
    </form>
</body>
</html>

Open in new window

RusselMetalsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
oldCookie.Expires < DateTime.Now

You want to check if the expiration date < the current time.  If so the cookie has expired.


 ElseIf (oldCookie.Expires < DateTime.Now) Then          ' cookie is valid


        ElseIf oldCookie.Expires > DateTime.Now Then         ' COOKIE EXPIRED.




The clearer way to write this would be:

 ElseIf (DateTime.Now < oldCookie.Expires  )  ' cookie is valid

elseif (dateTime.Now > oldCookie.Expires) ' Cookie has expired.
0
RusselMetalsAuthor Commented:
I found the error was that oldCookie.Expires  was always set to 1/1/0001 12:00 AM. So I changed the subroutine and now it works fine.

  Private Sub get_cookie()

        Dim saved_lang As String
        Dim oldCookie As HttpCookie = Request.Cookies(cookiename)

        If oldCookie Is Nothing Then                            ' no cookie was found
            Response.Write("There is no Cookie <br/>")
            Label1.Text = "None"
            None.Checked = True
        ElseIf (oldCookie IsNot Nothing) Then                   ' cookie is there
            saved_lang = oldCookie.Values("Language").ToString()
            Response.Write("<br /><br />")
            If saved_lang <> "" Then
                Label1.Text = saved_lang
                If saved_lang = "English" Then
                    English.Checked = True
                ElseIf saved_lang = "French" Then
                    French.Checked = True
                Else
                    None.Checked = True
                End If
            Else
                Response.Write("Cookie was Expired or not Available <br/>")
                Label1.Text = "None"
                None.Checked = True
            End If
        End If

    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RusselMetalsAuthor Commented:
I found the answer myself as the logic of my programming was correct but the reporting of the cookie expiration date in asp.net was not as expected
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.