ASP.NET C# Cookie Problem

Hi,
I am pretty new with ASP.Net, and I am trying to make a cookie that can keep 5 values (the ones I can re-user later). The issue here is that the page is creating 5 cookies with 1 value each instead of just 1 cookie with 5 values (I think).
This is the code:

'fDesde' = From (Date)
'fHasta' = To (Date)
Project
Path
Username

        protected void Page_Load(object sender, EventArgs e)
        {
            string fDesde = "";
            string fHasta = "";
            string Project = string.Empty;
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            string UserName = "";

            if (!this.IsPostBack)
            {
                if (Request.Cookies["sgdoc"]["Proyecto"] != null)
                {
                    Project = Request.Cookies["sgdoc"]["Proyecto"];
                }

                if (Request.Cookies["sgdoc"]["fDesde"] != null)
                {
                    fDesde = Request.Cookies["sgdoc"]["fDesde"];
                }

                if (Request.Cookies["sgdoc"]["fHasta"] != null)
                {
                    fHasta = Request.Cookies["sgdoc"]["fHasta"];
                }

                if (Session["UserName"] != null)
                {
                    UserName = Session["UserName"].ToString();
                    lblUser.Text = UserName + "<br>" + DateTime.Now;
                }
                

                ddlProyecto.Items.Insert(0, new ListItem("...", "0"));
                int cnt = 0;
                string proyecto = "";
                string temp = "";
                int limit = 0;

                if (Response.Cookies["lstPry" + UserName].Values.Count > 0)
                {
                    limit = Request.Cookies.Count;
                    for(cnt=1; cnt<=limit; cnt++)
                    {
                        proyecto = Response.Cookies["lstPry" + UserName]["nro" + cnt];
                        ddlProyecto.Items.Insert(cnt, new ListItem(proyecto));
                    }
                    
                }
                else
                {
                    HttpCookie aCookie = new HttpCookie("lstPry" + UserName);

                    Querys result = new Querys();
                    DataTable data = result.Projects(UserName);

                    foreach (DataRow pRow in data.Rows)
                    {
                        string[] tmp = pRow["Proyecto"].ToString().Split('-');
                        proyecto = tmp[0];

                        if (proyecto != temp)
                        {
                            cnt++;
                            ddlProyecto.Items.Insert(cnt, new ListItem(proyecto));

                            aCookie.Values["nro"+cnt] = proyecto;

                            temp = proyecto;
                        }
                    }
                    aCookie.Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Add(aCookie);
                }         

                /* *** */

                if (!string.IsNullOrEmpty(Project))
                {
                    ddlProyecto.Items.FindByValue(Project).Selected = true;
                }
                if (fDesde != null)
                {
                    this.FechaDesde.Text = fDesde;
                }
                if (fHasta != null)
                {
                    this.FechaHasta.Text = fHasta;
                }

Open in new window


        protected void btnHome_Click(object sender, EventArgs e)
        {
            Response.Cookies["sgdoc"]["Proyecto"] = "";
            Response.Cookies["sgdoc"]["fDesde"] = "";
            Response.Cookies["sgdoc"]["fHasta"] = "";
            Response.Redirect("/");
        }

Open in new window


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string proyecto = this.ddlProyecto.Text;
            string fDesde = this.FechaDesde.Text;
            string fHasta = this.FechaHasta.Text;
            string UserName = Session["UserName"].ToString();

            HttpCookie aCookie = new HttpCookie("sgdoc");
            aCookie.Values["Proyecto"] = proyecto;
            aCookie.Values["fDesde"] = fDesde;
            aCookie.Values["fHasta"] = fHasta;
            aCookie.Values["UserName"] = UserName;
            aCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(aCookie);

            try
            {
                Response.Redirect("Dashboard.aspx?Project=" + proyecto + "&fDesde=" + fDesde + "&fHasta=" + fHasta);
            }
            catch (Exception ex)
            {
                lblUser.Text = ex.ToString();
            }
        }

Open in new window


        protected void btnExit_Click(object sender, EventArgs e)
        {

            HttpCookie aCookie = Request.Cookies["sgdoc"];
            aCookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(aCookie);

            Response.Redirect("Login/default.aspx");
        }

Open in new window

LVL 2
José PerezAsked:
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.

Paul MacDonaldDirector, Information SystemsCommented:
There's no problem creating more than one cookie, but if you're set on having more than one value (subkeys) stored in each cookie, you can do it this way:

Response.Cookies["userInfo"]["userName"] = "patrick";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
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
José PerezAuthor Commented:
Wow, haven't seen the link you sent. Thanks for that, it works for me.
0
José PerezAuthor Commented:
The link is better for us, currently.
0
Paul MacDonaldDirector, Information SystemsCommented:
Thank you for the points.   Very happy to have helped.
1
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.