473,411 Members | 2,272 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,411 software developers and data experts.

Problem getting cookie from code-behind (worked in code-beside)

Hello,

I am just converting one of my pages from code-beside to code-behind. I
removed all the C# and put that in a separate file (Basket.cs) and added
references for all the ASP controls on the page. I changed the Page
references to "using" and added a class around the code.

When I tried to run the page, I got an error when I tried to reference
Request.Cookies. I remembered form a previous time that you have to use
HttpContext.Current to get at the Request object, so I added that.
Trouble is, it now gives me an error "Object reference not set to an
instance of an object" on the line that tries to get the cookie value.

I have shown a very simplified version of the .cs file below. Anyone any
idea what the problem is? The DisplayBasket method is called when the
page first loads, and it is the first line of this that causes the
problem. I have snipped the rest for clarity.

Thanks for any help.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ... a few others

public class Basket : Page {

protected Literal litSiteName; // plus a whole load more

void DisplayBasket() {
int basketId = Convert.ToInt32(HttpContext.Current.Request.Cookie s["BasketID"].Value);
// do other stuff
}
}

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
1 1438
<snip>
int basketId =
Convert.ToInt32(HttpContext.Current.Request.Cooki es["BasketID"].Value);

</snip>

I should mention that I know the cookie exists. I've been using it for a
couple of days when the code was in the same file. The problem has only
arisen now I've moved the code to a separate file.

I know I should check to see if the cookie exists first, but that isn't
the problem in this case.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #2

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

Similar topics

2
by: Sarah Michael | last post by:
Hi, I am running the following code to set a cookie but it is not working, please help me sorting out the problem. The code is from an online article http://www.devarticles.com/art/1/639 I...
1
by: Darrel Yurychuk | last post by:
I'm having a problem getting the window.onresize to work properly. Here is a simple test case I wrote: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">...
2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
3
by: Epetruk | last post by:
Hi, I'm trying to design a simple web form, but I cannot get the code in the btnSubmit_ServerClick event to fire. What am I doing wrong? Here's the codebehind: using System; using...
1
by: Michael | last post by:
Hello, I'm trying to implement sample I found on page templates, so I do not have to maintain "<html><head>..." on all my documents. But don't let that confuse you, this is an inheritance...
3
by: rss | last post by:
SUMMARY: ========== I am unable to pass along a simple Cookie obtained from a HttpWebRequest call (Machine A's ASP.NET app) so that another Web Server (Machine B) recongnizes the cookie. I...
0
by: Denver Developer | last post by:
I have a cookie that is set by a previous ASP page that I wish to update with the following logic. MyCookieColl = Request.Cookies; for (loop1 = 0; loop1 < MyCookieColl.Count ; loop1++) {...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
0
by: microcool1024 | last post by:
I am having a problem getting a C callback to correctly activate in a C# program after using a dllimport. Here is the declaration of the C code in the C header: typedef BOOL (CALLBACK...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.