user2461116
user2461116

Reputation: 19

The "clsDataLayer" does not exists in current context

I have been trying to figure this out for hours now, I know there is some small error that I have here but I am not able to pinpoint it. Please help me. So I created a class called "clsDataLayer.cs" and it is in the App_Code asp.net folder. I then created a form called "frmUserActivity", but now when I post the code in it and call the class it says "The "clsDataLayer" does not exists in current context" Can anyone please help me?

code for clsDataLayer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// Add your comments here
using System.Data.OleDb;
using System.Net;
using System.Data;

namespace ASP_PayRoll.App_Code
{
    public class clsDataLayer
    {
        // This function gets the user activity from the tblUserActivity
        public static dsUserActivity GetUserActivity(string Database)
        {
            // Add your comments here
            dsUserActivity DS;
            OleDbConnection sqlConn;
            OleDbDataAdapter sqlDA;

            // Add your comments here
            sqlConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Database);

            // Add your comments here
            sqlDA = new OleDbDataAdapter("select * from tblUserActivity", sqlConn);

            // Add your comments here
            DS = new dsUserActivity();

            // Add your comments here
            sqlDA.Fill(DS.tblUserActivity);

            // Add your comments here
            return DS;
        }

        // This function saves the user activity
        public static void SaveUserActivity(string Database, string FormAccessed)
        {
            // Add your comments here
            OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Database);
            conn.Open();
            OleDbCommand command = conn.CreateCommand();
            string strSQL;

            strSQL = "Insert into tblUserActivity (UserIP, FormAccessed) values ('" +
                GetIP4Address() + "', '" + FormAccessed + "')";

            command.CommandType = CommandType.Text;
            command.CommandText = strSQL;
            command.ExecuteNonQuery();
            conn.Close();
        }

        // This function gets the IP Address
        public static string GetIP4Address()
        {
            string IP4Address = string.Empty;

            foreach (IPAddress IPA in
                        Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != string.Empty)
            {
                return IP4Address;
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return IP4Address;
        }
    }
}

Code for frmUserActivity.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASP_PayRoll
{
    public partial class frmUserActivity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Declare the DataSet
                dsUserActivity myDataSet = new dsUserActivity();

                //Fill the dataset with what is returned from the function
                myDataSet = clsDataLayer.GetUserActivity(Server.MapPath("PayrollSystem_DB.mdb"));

                //Sets the DataGrip to the DataSource based on the table
                grdUserActivity.DataSource = myDataSet.Tables["tblUserActivity"];

                //Binds the DataGrid
                grdUserActivity.DataBind();
            }
        }
    }
}

Thanks in advance. Ak

Upvotes: 0

Views: 997

Answers (2)

kostas ch.
kostas ch.

Reputation: 2035

Use ASP_PayRoll.App_Code.clsDataLayer. Your namespace in your data layer class is ASP_PayRoll.App_Code and in your page it's ASP_PayRoll.

Upvotes: 3

Mansfield
Mansfield

Reputation: 15140

Add using ASP_PayRoll.App_Code; to your aspx file.

Since you declared that in a namespace other than the one of your aspx page, you have to use a using statement, or the entire path (ASP_PayRoll.App_Code.clsDataLayer) in order to be able to access that class.

Upvotes: 1

Related Questions