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

How to make a class?

c# .NET
I want a method that works in every page in my website

I have done this so far

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Web.Sessionstate;
  12.  
  13. public class SafeClass
  14. {
  15.     public bool Safepage()
  16.     {
  17.         bool safe = true;
  18.  
  19.         if (Session["USERID"] == null) safe = false;
  20.         else
  21.         {
  22.             if (Request.QueryString["ID"] != null)
  23.             {
  24.  
  25.                 if (Request.QueryString["ID"] != Session["USERID"].ToString()) safe = false;
  26.  
  27.             }
  28.             else
  29.                 safe = false;
  30.         }
  31.         return safe;
  32.     }
  33. }
I get these errors
  • The name 'Session' does not exist in the current context
  • The name 'Request' does not exist in the current context
  • The name 'Request' does not exist in the current context
  • The name 'Session' does not exist in the current context


Why?

I am using System.Web.Sessionstate. so it should find Session. when i have the same code in a page it works.

please help me i have search for help with this but havn't found it.
Sep 27 '06 #1
2 2318
axas
32
Suppose you have a Login.aspx page with that code:

protected void Page_Load(object sender, EventArgs e) {
if ( Session["Username"] != null )
Response.Redirect("Admin.aspx");
}

// after clicking the login button
protected void btnLogin_Click(object sender, EventArgs e) {
int valid = Administrator.Login(txtUsername.Text, txtPassword.Text);

if ( valid != 0 ) {
Session["Username"] = txtUsername.Text;
Response.Redirect("Admin.aspx");
}
else {
lblMesage.Text = "Wrong password, username............";
}
}


... And a Admin.cs file with this code:

public class Administrator
{
private SqlCommand cmd;

public Administrator()
{

}

public static int Login(string username, string password)
{
cmd = new SqlCommand(@"SELECT COUNT(*) FROM tableX WHERE
username=@username and password=@password", ConnectionObject);
cmd.Parameters.Add("@username", SqlDbType.Varchar, 20, "username");
cmd.Parameters.Add("@password", SqlDbType.Varchar, 20, "password");
cmd.Parameters["@username"].Value = username;
cmd.Parameters["@password"].Value = password;
return cmd.ExecuteScalar();
}
}

Namely, you can't have a Session object in a class.
Objects like Session or Request, can' t be in classes, but only in WebPages and WebUserControls.
Sep 28 '06 #2
Namely, you can't have a Session object in a class.
Objects like Session or Request, can' t be in classes, but only in WebPages and WebUserControls.
[/quote]

Thanks. All i needed to know.
Sep 28 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
3
by: craig | last post by:
Given two existing but different classes OldA and OldB (that can not be made to derive from any new base class); is there a way to make them both "observer" objects so that they can be put in one...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
4
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
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
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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...

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.