How to make a class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Metalzed
    New Member
    • Sep 2006
    • 27

    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

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Sessionstate;
    
    public class SafeClass
    {
        public bool Safepage()
        {
            bool safe = true;
            
            if (Session["USERID"] == null) safe = false;
            else
            {
                if (Request.QueryString["ID"] != null)
                {
    
                    if (Request.QueryString["ID"] != Session["USERID"].ToString()) safe = false;
    
                }
                else
                    safe = false;
            }
            return safe;
        }
    }
    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.Sess ionstate. 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.
  • axas
    New Member
    • Jul 2006
    • 32

    #2
    Suppose you have a Login.aspx page with that code:

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

    // after clicking the login button
    protected void btnLogin_Click( object sender, EventArgs e) {
    int valid = Administrator.L ogin(txtUsernam e.Text, txtPassword.Tex t);

    if ( valid != 0 ) {
    Session["Username"] = txtUsername.Tex t;
    Response.Redire ct("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(@"SE LECT COUNT(*) FROM tableX WHERE
    username=@usern ame and password=@passw ord", ConnectionObjec t);
    cmd.Parameters. Add("@username" , SqlDbType.Varch ar, 20, "username") ;
    cmd.Parameters. Add("@password" , SqlDbType.Varch ar, 20, "password") ;
    cmd.Parameters["@username"].Value = username;
    cmd.Parameters["@password"].Value = password;
    return cmd.ExecuteScal ar();
    }
    }

    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 .

    Comment

    • Metalzed
      New Member
      • Sep 2006
      • 27

      #3
      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.

      Comment

      Working...