0

I'm using a public LoginContext class to manage user logins in my web app.

Unfortunately, even though I have the LoginContext class declared publicly, my partial class Login at Login.aspx.cs can't seem to access it.

My code is as follows:

// ~/App_Code/LoginContext.cs
namespace stman
{
    public class LoginContext
    {

    }
}

// ~/Login.aspx.cs
namespace stman
{
    public partial class Login : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            LoginContext log = new LoginContext(); // error is here
        }
    }
}

The error that comes up on the line where I instantiate LoginContext reads as follows:

The type or namespace name 'LoginContext' could not be found (are you missing a using directive or an assembly reference?)

When I try to generate a new class for LoginContext, it goes into the web app's root folder where it can no longer access the public Database class that I need in LoginContext.

I have no idea what's causing all of these problems, but based on what I've learned over the last 18 months doing this professionally, they shouldn't exist right now...

Can anyone help clear things up here? Specifically I'd like to know:

  1. What I'm doing wrong
  2. Why it's wrong
  3. Who can I fix it?

Thanks in advance!

EDIT
I've had a look and it seems neither the Database class in ~/App_Code/Database.cs or the LoginContext class in ~/App_Code/LoginContext.cs are accessible to the page - or any page in the website.

7
  • Try adding namespace of the LoginContext class
    – Nag
    Jun 12, 2013 at 11:30
  • 2
    If you check LoginContext.cs properties, is it marked as BuildAction = Compile ? Jun 12, 2013 at 11:34
  • @Nag didn't help. LoginContext does not appear to be a part of the namespace, even though I can see that it clearly is
    – Ortund
    Jun 12, 2013 at 11:36
  • @OndrejSvejdar BuildAction for LoginContext.cs is Content
    – Ortund
    Jun 12, 2013 at 11:36
  • Switch it to compile. Jun 12, 2013 at 11:38

4 Answers 4

2

In LoginContext.cs properties, marked it as BuildAction = Compile.

1
  • Also had to do this for Database.cs ... I'm not sure what this is changing exactly, but it did fix my problem
    – Ortund
    Jun 12, 2013 at 13:32
0

You can achieve this behaviour when these classes are located in different projects.

If this is true then you should use full path to the class starting from the project name ProjectName.stman.LoginContext

0
0

try using constructor

 namespace stman
{
// Database is a class that handles the sql queries and such
public class LoginContext : Database
{
    public LoginContext () : base("Name=LoginContext")
    {

    }
}
}
1
  • Didn't work, got an error under base("Name=LoginContext") saying 'object' does not contain a constructor that contains 1 arguments.. Had a look over my code and realized I don't need to be inheriting Database after all
    – Ortund
    Jun 12, 2013 at 11:35
0

From what I read from the App_Code behavior may be different in web site projects and web application. I wonder what kind of project you're working on? One possible solution would be to make this project in web application project, this Link can help you to make this project:

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.