Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 4th, 2008, 03:58 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default The problem during writing into an existing cookie

The problem during writing into an existing cookie, after re running my web application

hi to all forums members

can any one tell me what is error in mine code, It create every time a new cookie, but i have to write into existing cookie.
When i close my web application and re running my web application and, adding values into cookies it creates new cookies... why I m unable to understand....

following is my code...
protected void btnAddCokkie_Click(object sender, EventArgs e)
    {
        CreateAndAddCookiesValue();
    }
    private void CreateAndAddCookiesValue()
    {
        //Response.Cookies.Remove("ShowHistoryCookie");
        if (Request.Browser.Cookies == true)
        {
            // Check the cookie Mycookie exist or not
            if (Request.Cookies["ShowHistoryCookie"] == null)
            {
                // If does Mycookie does not exist then create the cookie
                HttpCookie aShowHistoryCookie = new HttpCookie("ShowHistoryCookie");
                Response.Cookies.Add(aShowHistoryCookie);

                // set expires to the datetime maxvalue
                Response.Cookies["ShowHistoryCookie"].Expires = DateTime.Now.AddMonths(1);
                //Response.Cookies["ShowHistoryCookie"].Expires = DateTime.MaxValue; //DateTime.Now.AddMonths(1)

                Response.Cookies["ShowHistoryCookie"]["URL"] += txtUrlLink.Text;
            }
            //Append UrlLink into existing cookie
            else
            {
                string strCookie = Request.Cookies["ShowHistoryCookie"]["URL"].ToString();
                string strCook = strCookie;
                strCookie += "|" + txtUrlLink.Text;
                char[] splitter = { '|' };
                string[] arInfo = new string[10];

                arInfo = strCook.Split(splitter);
                int count = arInfo.Length;
                int intResult=-1;
                bool flag = false;

                //count to Restrict cookie to Store 10 UrlLink
                if (count <10)
                {
                    //check to ensure that no duplicacy in UrlLink of cookies
                    foreach (string str in arInfo)
                    {
                        intResult = str.CompareTo(txtUrlLink.Text);
                        if (intResult == 0)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag==false)
                    {
                        Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
                    }
                }
        //Sorting cookies to ensure that only ten recents Url link are added only...
                else if (count >= 10)
                {
                    foreach (string str in arInfo)
                    {
                        intResult = str.CompareTo(txtUrlLink.Text);
                        if (intResult == 0)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == false)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            //Swaping
                            if (i + 1 >= 10)
                            {
                                arInfo[9] = txtUrlLink.Text;
                                break;
                            }
                            else
                            {
                                arInfo[i] = arInfo[i + 1];
                            }
                        }
                        strCookie = arInfo[0] + "|" + arInfo[1] + "|" + arInfo[2] + "|" + arInfo[3] + "|" + arInfo[4] + "|" + arInfo[5] + "|" + arInfo[6] + "|" + arInfo[7] + "|" + arInfo + "|" + arInfo[9];
                        Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
                    }
                }
            }
        }
        else
        {
            // Else display message that
            // the browser does not accept cookies
            Response.Write("This Browser does not accept cookies");
        }

  #2 (permalink)  
Old March 4th, 2008, 07:12 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. how do you close your web app and re open it??

are you testing the site always calling it from the same place???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #3 (permalink)  
Old March 5th, 2008, 02:45 AM
Authorized User
 
Join Date: Nov 2007
Location: Allahabad, UP, India.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello sir
I am running the application from my PC and also the application is on my machine and i am closing the application directly from the close button..
Please try to solve my problem why cookie is deleted when i close my application even i set the expires time to 1 month...

following is my new code...

protected void btnAddCokkie_Click(object sender, EventArgs e)
    {
        CreateAndAddCookiesValue();
    }
    private void CreateAndAddCookiesValue()
    {
        //Response.Cookies.Remove("ShowHistoryCookie");
        if (Request.Browser.Cookies == true)
        {
            // Check the cookie Mycookie exist or not
            HttpCookie aShowHistoryCookie = Request.Cookies["ShowHistoryCookie"];
            if(aShowHistoryCookie == null) //if (Request.Cookies["ShowHistoryCookie"].Value == null)
            {
                // If Mycookie does not exist then create the cookie
                aShowHistoryCookie = new HttpCookie("ShowHistoryCookie");
                aShowHistoryCookie.Expires = DateTime.Now.AddMonths(1);
                Response.Cookies.Add(aShowHistoryCookie);

            }
                if (Request.Cookies["ShowHistoryCookie"]["URL"] == null)
                {
                    // set expires to the datetime maxvalue

                    Response.Cookies["ShowHistoryCookie"]["URL"] += txtUrlLink.Text;
                }

            //Append UrlLink into existing cookie
                else
                {
                    string strCookie = Request.Cookies["ShowHistoryCookie"]["URL"].ToString();
                    string strCook = strCookie;
                    strCookie += "|" + txtUrlLink.Text;
                    char[] splitter = { '|' };
                    string[] arInfo = new string[10];

                    arInfo = strCook.Split(splitter);
                    int count = arInfo.Length;
                    int intResult = -1;
                    bool flag = false;

                    //count to Restrict cookie to Store 10 UrlLink
                    if (count < 10)
                    {
                        //check to ensure that no duplicacy in UrlLink of cookies
                        foreach (string str in arInfo)
                        {
                            intResult = str.CompareTo(txtUrlLink.Text);
                            if (intResult == 0)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == false)
                        {
                            Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
                        }
                    }
                    else if (count >= 10)
                    {
                        foreach (string str in arInfo)
                        {
                            intResult = str.CompareTo(txtUrlLink.Text);
                            if (intResult == 0)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == false)
                        {
                            for (int i = 0; i < 10; i++)
                            {
                                //Swaping
                                if (i + 1 >= 10)
                                {
                                    arInfo[9] = txtUrlLink.Text;
                                    break;
                                }
                                else
                                {
                                    arInfo[i] = arInfo[i + 1];
                                }
                            }
                            strCookie = arInfo[0] + "|" + arInfo[1] + "|" + arInfo[2] + "|" + arInfo[3] + "|" + arInfo[4] + "|" + arInfo[5] + "|" + arInfo[6] + "|" + arInfo[7] + "|" + arInfo + "|" + arInfo[9];
                            Response.Cookies["ShowHistoryCookie"]["URL"] = strCookie;
                        }

                    }
                    Response.Write("From Code behind: "+strCookie);
                }
        }
        else
        {
            // Else display message that
            // the browser does not accept cookies
            Response.Write("This Browser does not accept cookies");
        }

Ragards
Vishnu

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookie problem ps0208 Servlets 1 November 5th, 2007 05:55 AM
Existing Cookie operation after cookies disabled John K. King Classic ASP Basics 1 April 2nd, 2006 03:44 AM
Cookie Problem jobiweb ASP.NET 1.0 and 1.1 Basics 0 May 12th, 2005 06:10 AM
COOKIE problem Buravik PHP How-To 2 May 9th, 2004 08:47 AM



All times are GMT -4. The time now is 05:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.