473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Loading a cookie on Page_load (ASP.NET)

Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-

Expand|Select|Wrap|Line Numbers
  1. HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  
  3. myCookie.Value = tbxLogin.Text;
  4. Response.Cookies.Add(myCookie);
  5.  
and I can retrieve the cookie (By pressing another button) using:-

Expand|Select|Wrap|Line Numbers
  1. HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2. getMyCookie = Request.Cookies["MyTestCookie"];
  3.  
  4. if (getMyCookie.Value != "")
  5. {
  6. lblName.Text = "Welcome " + getMyCookie.Value;
  7. }
  8.  
The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!

The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!

Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!

Thanks in advance

-Denna
Nov 16 '05 #1
6 9220
Hi Denna,

If i am you, I will do this:

HttpCookie cookies = Request.Cookies["MyTestCookie"].Value;

if (cookies != "")
{
lblName.Text = "Welcome " + cookies;
}

Correct me if i am wrong. I do not have VS.NET with me now, using a notepad :)
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-

Expand|Select|Wrap|Line Numbers
  1.  HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  myCookie.Value = tbxLogin.Text;
  3.  Response.Cookies.Add(myCookie);
  4.  

and I can retrieve the cookie (By pressing another button) using:-

Expand|Select|Wrap|Line Numbers
  1.  HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2.  getMyCookie = Request.Cookies["MyTestCookie"];
  3.  if (getMyCookie.Value != "")
  4.  {
  5.  lblName.Text = "Welcome " + getMyCookie.Value;
  6.  }
  7.  

The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!

The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!

Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!

Thanks in advance

-Denna

Nov 16 '05 #2
Sorry Chua, it didnt work, I get two errors when I try to build the solution, as a note, I'm running Visual Studio 7, so that may be the reason!!! :)

"Chua Wen Ching" wrote:
Hi Denna,

If i am you, I will do this:

HttpCookie cookies = Request.Cookies["MyTestCookie"].Value;

if (cookies != "")
{
lblName.Text = "Welcome " + cookies;
}

Correct me if i am wrong. I do not have VS.NET with me now, using a notepad :)
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-

Expand|Select|Wrap|Line Numbers
  1.  > HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  >
  3.  > myCookie.Value = tbxLogin.Text;
  4.  > Response.Cookies.Add(myCookie);
  5.  > 

and I can retrieve the cookie (By pressing another button) using:-

Expand|Select|Wrap|Line Numbers
  1.  > HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2.  > getMyCookie = Request.Cookies["MyTestCookie"];
  3.  >
  4.  > if (getMyCookie.Value != "")
  5.  > {
  6.  > lblName.Text = "Welcome " + getMyCookie.Value;
  7.  > }
  8.  > 

The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!

The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!

Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!

Thanks in advance

-Denna

Nov 16 '05 #3
Hi Denna,

Can you paste the 2 errors here? Is it a compile or run time error?

Thanks. Sorry for the trouble.
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Sorry Chua, it didnt work, I get two errors when I try to build the solution, as a note, I'm running Visual Studio 7, so that may be the reason!!! :)

"Chua Wen Ching" wrote:
Hi Denna,

If i am you, I will do this:

HttpCookie cookies = Request.Cookies["MyTestCookie"].Value;

if (cookies != "")
{
lblName.Text = "Welcome " + cookies;
}

Correct me if i am wrong. I do not have VS.NET with me now, using a notepad :)
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-

Expand|Select|Wrap|Line Numbers
  1.  > > HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  > >
  3.  > > myCookie.Value = tbxLogin.Text;
  4.  > > Response.Cookies.Add(myCookie);
  5.  > > 

and I can retrieve the cookie (By pressing another button) using:-

Expand|Select|Wrap|Line Numbers
  1.  > > HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2.  > > getMyCookie = Request.Cookies["MyTestCookie"];
  3.  > >
  4.  > > if (getMyCookie.Value != "")
  5.  > > {
  6.  > > lblName.Text = "Welcome " + getMyCookie.Value;
  7.  > > }
  8.  > > 

The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!

The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!

Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!

Thanks in advance

-Denna

Nov 16 '05 #4
Hiya Chau, sorry for the delay, I had to go to bed, dreaming about cookies would you believe, anyway, it gives off 2 compile time errors:-

"c:\inetpub\wwwroot\index\WebForm1.aspx.cs(82) : Cannot implicitly convert type 'string' to 'System.Web.HttpCookie'"

and

"c:\inetpub\wwwroot\index\WebForm1.aspx.cs(84) : Operator '!=' cannot be applied to operands of type 'System.Web.HttpCookie' and 'string'"

'QUOTE: Thanks. Sorry for the trouble.'

Don't worry Chau, lol, I'm just over the moon someone is helping me with it, I really appreciate you using your time on this!!! ;)

-Denna

I put your code in the part where, if you click a button, it's supposed to read the cookie (Though, my original code works fine here), I really want to read the cookie when the ASP.NET page loads up, and display the users name.


"Chua Wen Ching" wrote:
Hi Denna,

Can you paste the 2 errors here? Is it a compile or run time error?

Thanks. Sorry for the trouble.
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Sorry Chua, it didnt work, I get two errors when I try to build the solution, as a note, I'm running Visual Studio 7, so that may be the reason!!! :)

"Chua Wen Ching" wrote:
Hi Denna,

If i am you, I will do this:

HttpCookie cookies = Request.Cookies["MyTestCookie"].Value;

if (cookies != "")
{
lblName.Text = "Welcome " + cookies;
}

Correct me if i am wrong. I do not have VS.NET with me now, using a notepad :)
--
Regards,
Chua Wen Ching :)
"Denna" wrote:

> Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-
>
>
Expand|Select|Wrap|Line Numbers
  1.  > > > HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  > > >
  3.  > > > myCookie.Value = tbxLogin.Text;
  4.  > > > Response.Cookies.Add(myCookie);
  5.  > > > 
>
> and I can retrieve the cookie (By pressing another button) using:-
>
>
Expand|Select|Wrap|Line Numbers
  1.  > > > HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2.  > > > getMyCookie = Request.Cookies["MyTestCookie"];
  3.  > > >
  4.  > > > if (getMyCookie.Value != "")
  5.  > > > {
  6.  > > > lblName.Text = "Welcome " + getMyCookie.Value;
  7.  > > > }
  8.  > > > 
>
> The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!
>
> The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!
>
> Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!
>
> Thanks in advance
>
> -Denna

Nov 16 '05 #5
Hi Denna,

Hope this solution can help you. But there are many ways to do it. This is just an example. :)

Codes:

protected HttpCookie cookie;

private void Page_Load(object sender, System.EventArgs e)
{
cookie = new HttpCookie("MyTestCookie");
cookie.Value = "Chua Wen Ching";
cookie.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie);
}

private void Button1_Click(object sender, System.EventArgs e)
{
HttpCookieCollection cookieCollection = new HttpCookieCollection();
cookieCollection = Request.Cookies;

string retrieveCookie = "";

foreach(string str in cookieCollection)
{
retrieveCookie = Request.Cookies[str].Value;
}

if (retrieveCookie != null)
{
Label1.Text = "Welcome " + retrieveCookie;
}
else
{
cookie = new HttpCookie("MyTestCookie");
cookie.Value = "Chua Wen Ching";
cookie.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie);
}
}

Finally i had vs.net with me. Haha! Anyway please let me know if this is not what you want. This might not be the best solution. :)
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Hiya Chau, sorry for the delay, I had to go to bed, dreaming about cookies would you believe, anyway, it gives off 2 compile time errors:-

"c:\inetpub\wwwroot\index\WebForm1.aspx.cs(82) : Cannot implicitly convert type 'string' to 'System.Web.HttpCookie'"

and

"c:\inetpub\wwwroot\index\WebForm1.aspx.cs(84) : Operator '!=' cannot be applied to operands of type 'System.Web.HttpCookie' and 'string'"

'QUOTE: Thanks. Sorry for the trouble.'

Don't worry Chau, lol, I'm just over the moon someone is helping me with it, I really appreciate you using your time on this!!! ;)

-Denna

I put your code in the part where, if you click a button, it's supposed to read the cookie (Though, my original code works fine here), I really want to read the cookie when the ASP.NET page loads up, and display the users name.


"Chua Wen Ching" wrote:
Hi Denna,

Can you paste the 2 errors here? Is it a compile or run time error?

Thanks. Sorry for the trouble.
--
Regards,
Chua Wen Ching :)
"Denna" wrote:
Sorry Chua, it didnt work, I get two errors when I try to build the solution, as a note, I'm running Visual Studio 7, so that may be the reason!!! :)

"Chua Wen Ching" wrote:

> Hi Denna,
>
> If i am you, I will do this:
>
> HttpCookie cookies = Request.Cookies["MyTestCookie"].Value;
>
> if (cookies != "")
> {
> lblName.Text = "Welcome " + cookies;
> }
>
> Correct me if i am wrong. I do not have VS.NET with me now, using a notepad :)
> --
> Regards,
> Chua Wen Ching :)
>
>
> "Denna" wrote:
>
> > Hello, I'm having a little trouble with cookies, I can write one when I press a button using the following code:-
> >
> >
Expand|Select|Wrap|Line Numbers
  1.  > > > > HttpCookie myCookie = new HttpCookie("MyTestCookie");
  2.  > > > >
  3.  > > > > myCookie.Value = tbxLogin.Text;
  4.  > > > > Response.Cookies.Add(myCookie);
  5.  > > > > 
> >
> > and I can retrieve the cookie (By pressing another button) using:-
> >
> >
Expand|Select|Wrap|Line Numbers
  1.  > > > > HttpCookie getMyCookie = new HttpCookie("MyTestCookie");
  2.  > > > > getMyCookie = Request.Cookies["MyTestCookie"];
  3.  > > > >
  4.  > > > > if (getMyCookie.Value != "")
  5.  > > > > {
  6.  > > > > lblName.Text = "Welcome " + getMyCookie.Value;
  7.  > > > > }
  8.  > > > > 
> >
> > The problem is, I want to check if a cookie exists when the page loads up, if so, then I want to write the value (Which is just a persons name), into a label!!!
> >
> > The problem is, when I try to use the second code snippet in the Page_Load(...) function, I get an error!!!
> >
> > Could anyone point out where I stick the second code snippet, or, if I'm going about this the wrong way, has anyone got a few pointers!!!
> >
> > Thanks in advance
> >
> > -Denna

Nov 16 '05 #6
It's not exactly what I'm after, but it's enough for me to grasp what's happening!!! :)

Thanks very much Chua for your help, I really do appreciate it!!!

All the best my friend

-Denna
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dafna | last post by:
while trying to open an Asp .Net application ' I get the following message (the IIS on my computer is running) "Visual Studio .Net has detected that the specified web server is not running ASP...
1
by: jm | last post by:
I am sure this is simple, but I cannot find it. I have a frameset with two frames. The top frame is a form to be filled out. The bottom frame is the resultset. In the top frame when I hit...
0
by: Kathryn | last post by:
Hello, When I try to load this xml file (see below) into an asp.net dataset using the following code: ds.ReadXml("xmlfilepath\xmlfile.xml", XmlReadMode.Auto) I get the following error: ...
0
by: Karuppasamy Natarajan via .NET 247 | last post by:
I am storing encrypted username and password in cookies in a .asppage. When I am trying to get these cookies and decrypting, I amnot the actual string values. I am using the same component...
3
by: Anavim | last post by:
Hi, I have a really wired behaviour in my asp.net application. Little background, I have a base pages which contains about 17 images laying around (create a circle) and this page contains a...
5
by: James | last post by:
hi, I'm trying to remember a user who has visited the page. what i found out was to use persistent cookie, is there any other way that i can remember the user next time he visit? When using...
1
by: dixonjm | last post by:
HI, I have to make a call to a central stand alone class, which will write auditing data to the database. I was wondering which would be the quickest way; either from a httpModule or via the...
1
by: mmorrison93 | last post by:
I'd like to see every function call that takes place when a page loads, from the Init method though to the method where the actual page rendering takes place. Is there a way to do this with ASP.NET...
2
by: SV | last post by:
I have a login form with “Remember me on this computer” checkbox. I am creating cookie if checkbox is checked. My code is: If chk_remember.Checked = True Then Dim ckUserName As HttpCookie =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.