0

I have a class called CatalogAccess in the app_Code folder and a DataList user control in a folder called UserControls which is in the root folder. The UserControl is used to display a list of departments that are being pulled in the CatalogAccess. I call the GetDepartments function in the CatalogAccess class like this:

list.DataSource = CatalogAccess.GetDepartments();
list.DataBind();

However, intelisense keeps telling me that

The name CatalogAccess does not exist in the current context

CatalogAccess class

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace techniccashregister.App_Code
{
    // Product catalog business tier component
    public static class CatalogAccess
    {
        static CatalogAccess()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static DataTable GetDepartments()
        {
            // Get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // Set the stored procedure name
            comm.CommandText = "GetDepartments";
            // Exectute the store procedure and return the results
            return GenericDataAccess.ExecuteSelectCommand(comm);
        }
    }
}

User Control

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

namespace techniccashregister.UserControls
{
    public partial class DepartmentsList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            list.DataSource = CatalogAccess.GetDepartments();
            list.DataBind();
        }
    }
}

Class Accessing data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace techniccashregister.App_Code
{
    // This class contains generic data access functionality to be accessed from the business tier
    public static class GenericDataAccess
    {
        // Static constructor
        static GenericDataAccess()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Executes a commmand and returns the results as a DataTable object
        public static DataTable ExecuteSelectCommand(DbCommand command)
        {
            // The DataTable to be returned
            DataTable table;
            // Execute the command, making sure the connection gets closed in the end
            try
            {
                // Open the data connection
                command.Connection.Open();
                // Execute the command and save the results in a DataTable
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);

                // Close the reader
                reader.Close();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw;
            }

            finally
            {
                // Close the connection
                command.Connection.Close();
            }

            return table;
        }

        // Creates and prepares the new DbCommand object on a new connection
        public static DbCommand CreateCommand()
        {
            // Obtain the database provider name
            string dataProviderName = technicConfiguration.DbProviderName;
            // Obtain the database connection string
            string connectionString = technicConfiguration.DbConnectionString;
            // Create the new data provider factory
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            // Obtain a database-specific connection object
            DbConnection conn = factory.CreateConnection();
            // Set the connection string
            conn.ConnectionString = connectionString;
            // Create a database-specific command object
            DbCommand comm = conn.CreateCommand();
            // Set the command type to stored procedure
            comm.CommandType = CommandType.StoredProcedure;
            // Return the initialized command object
            return comm;
        }
    }
}

Does anyone know what can I do to fix this? Thanks in advance

user3294748
  • 27
  • 2
  • 9
  • Hi T.S. For what I can see, both of them have the namespace. – user3294748 Jan 28 '15 at 15:26
  • Everything has namespace. There are two reasons, your class is not visibile - 1. It is in different namespace and your namespace is not imported. 2. your class is not public enough to be visible. Remember - you're in a web site project. If you use App_Code, you building many DLLs and your user control may not see whats in some class. Can you post declaration of your class ? – T.S. Jan 28 '15 at 15:32
  • using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.Common; namespace techniccashregister.App_Code { // Product catalog business tier component public static class CatalogAccess { – user3294748 Jan 28 '15 at 15:35
  • Sorry, that did not come out so good. That was the catalogAccess class. – user3294748 Jan 28 '15 at 15:36
  • using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace techniccashregister.UserControls { public partial class DepartmentsList : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) – user3294748 Jan 28 '15 at 15:37
  • This is the code behind the UserControl – user3294748 Jan 28 '15 at 15:38
  • 1
    @user3294748 You should poste code in the question itself, rather than trying to shoehorn it into comments. – mason Jan 28 '15 at 15:38
  • 1
    @user3294748 - Add to user control `using techniccashregister.App_Code;` – T.S. Jan 28 '15 at 15:40
  • It did not work. I am still getting the same error message. – user3294748 Jan 28 '15 at 15:42
  • Those are my complete classes. – user3294748 Jan 28 '15 at 15:48
  • 1
    Does this http://stackoverflow.com/a/1222293/1191903 help? – Kevin Main Jan 28 '15 at 15:53
  • Hi Kevin. I changed it from content to compile and it just threw more errors. – user3294748 Jan 28 '15 at 15:57
  • @user3294748 what sort of errors? – Kevin Main Jan 28 '15 at 16:00
  • Oh nvm. I just changed all the classes to compile and it works now. Thank you so much guys. You are great. – user3294748 Jan 28 '15 at 16:02
  • @KevinMain I don't even have this option in properties – T.S. Jan 28 '15 at 16:04
  • I am using VS 2012. If you click on your classes it should show Build Action as the third selection in properties. – user3294748 Jan 28 '15 at 16:12
  • @user3294748 I am also using vs2012. Is your project WSP or WAP? – T.S. Jan 28 '15 at 16:15
  • No, is just a regular project for school so I am just writing code in the file system without even using IIS or anything else. – user3294748 Jan 28 '15 at 16:27
  • Lets ask this... When you right-click on your project in solution explorer, does it say **Build** or **Build Web Site** in one of the menus on top? – T.S. Jan 28 '15 at 16:45
  • Yes, it is the first selection. Build then Rebuild, Publish... – user3294748 Jan 29 '15 at 02:55
  • Ah... That explains it. I thought you have WSP. You shouldn't even have App_Code folder to begin with. That confused me. In WAP you can better define your namespaces. Read this , all way to the end. http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html – T.S. Jan 29 '15 at 15:08

0 Answers0