locked
[IIS & VS] Cannot access class located in App_Code RRS feed

  • Question

  • User948428231 posted

    Hey all. I'm just beginning to read a book on ASP.NET 4 by Murach. To start with I've set up IIS on my machine, and what I'm trying to do is to set up an exercise that he has in the book. In the exercise folder there is no project file, which I believe means that this is a web site rather than a web application.

    So what I have done so far:
    1. Gone to IIS Manager
    2. Added the appropriate folder (Ch01Cart) as a virtual directory
    3. Accessed the Order.aspx file with the following url: 'http://localhost/Ch01Cart/Order.aspx'

    Once there I get the following error: "Compiler Error Message: CS0246: The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?)" What he was trying to do in the code there was simply to create an instance of the Product class from Order.aspx. The path for Product.cs is Ch01Cart/App_Code/Product.cs and the path for Order.aspx is Ch01Cart/Order.aspx. So essentially it can't access Product.cs from Order.aspx. I read somewhere that this meant that I need to change the build action on the files under App_Code to 'compile', but I also read that that only applied to Web Applications, and as I stated before, I think this is a website, even though I'm not certain about the differences. One thing that's odd is that, when I open the folder in Visual Studio (File -> Open -> Web Site -> Local -> Browse for folder) I can start debugging and it opens Order.aspx just fine.

    Here's the code for both files:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    
    public partial class Order : System.Web.UI.Page
    {
    private Product selectedProduct;
    
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    ddlProducts.DataBind();
    
    selectedProduct = this.GetSelectedProduct();
    lblName.Text = selectedProduct.Name;
    lblShortDescription.Text = selectedProduct.ShortDescription;
    lblLongDescription.Text = selectedProduct.LongDescription;
    lblUnitPrice.Text = selectedProduct.UnitPrice.ToString("c");
    imgProduct.ImageUrl = "Images/Products/" + selectedProduct.ImageFile;
    }
    
    private Product GetSelectedProduct()
    {
    DataView productsTable = (DataView)
    AccessDataSource1.Select(DataSourceSelectArguments.Empty);
    productsTable.RowFilter =
    "ProductID = '" + ddlProducts.SelectedValue + "'";
    DataRowView row = (DataRowView)productsTable[0];
    
    Product p = new Product();
    p.ProductID = row["ProductID"].ToString();
    p.Name = row["Name"].ToString();
    p.ShortDescription = row["ShortDescription"].ToString();
    p.LongDescription = row["LongDescription"].ToString();
    p.UnitPrice = (decimal)row["UnitPrice"];
    p.ImageFile = row["ImageFile"].ToString();
    return p;
    }
    
    protected void btnAdd_Click(object sender, EventArgs e)
    {
    CartItemList cart = CartItemList.GetCart();
    CartItem cartItem = cart[selectedProduct.ProductID];
    
    if (cartItem == null)
    cart.AddItem(selectedProduct, Convert.ToInt32(txtQuantity.Text));
    else
    cartItem.AddQuantity(Convert.ToInt32(txtQuantity.Text));
    
    Response.Redirect("Cart.aspx");
    }
    }

    public class Product
    {
    public string ProductID { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    public decimal UnitPrice { get; set; }
    public string ImageFile { get; set; }
    }



    Does anyone know what the issue might be?

    Thursday, July 4, 2013 3:02 PM

All replies

  • User1759999623 posted

    You need to explicitly add a namespace section into both files, making them the same namespace.  Otherwise, the compiler will try to assume what the namespace is supposed to be.

    So, do this:

    namespace MyNamespace
    {
       public partial class Order : System.Web.UI.Page
       {
          ...
       }
    }
    Thursday, July 4, 2013 4:49 PM
  • User1482735129 posted

    Make sure that you didnt select web application instead of website when you created the project.

    You can try wrapping your product class with a namespace then add the using statement in your orders class or you can full qualify your instance of the product class.

    IN your orders class

    Wrap the products class with a namespace

    namespace Products
    {
        public class Product
        {
            public string ProductID { get; set; }
            public string Name { get; set; }
            public string ShortDescription { get; set; }
            public string LongDescription { get; set; }
            public decimal UnitPrice { get; set; }
            public string ImageFile { get; set; }
        }
    }

    In your orders class  add a using statement

    using Products;

     

    Thursday, July 4, 2013 4:54 PM
  • User948428231 posted

    Thank you for your response Careed. I've tried adding a namespace everywhere and more and more errors seem to pop up. So what I tried was spafa9's solution: I added a namespace on the Product class and added "using Product;" to Order.aspx.cs. The only thing that changed was that now it stops at the using directive. 

    As for the errors I have when I try to add the namespace everywhere: "ASP.cart_aspx.GetTypeHashCode(): no suitable method found to override.

    ASP.cart_aspx does not implement interface member 'System.Web.IHttpHandler.IsReusable'" and more.

    Do you have any idea why it works without namespaces when I launch it from Visual Studio, but not when I try to access it through the localhost URL? 

    Friday, July 5, 2013 4:45 AM
  • User948428231 posted

    I didn't create the project, I can have a look, but all I know is that there isn't a project file in there, which I think means I'm dealing with a web site. I also tried your solution as mentionied in my other reply, but it stopped at the using directive instead. 

    Friday, July 5, 2013 5:33 AM
  • User1482735129 posted
    In you ASPx page switch to HTML in the directives make sure that the code file also has The namespace. It sounds like the page can't find the code.
    Friday, July 5, 2013 5:53 AM
  • User948428231 posted

    Could this be an IIS issue? I have a friend who ran the web site just fine from his computer. It's worth noting that I have installed Apache on my computer previously, but I stopped the server before running the IIS. Could there be a conflict there of some sort?

    Edit: I don't see the IIS page when I go to localhost (it's blank).

    Friday, July 5, 2013 7:02 AM
  • User1482735129 posted

    First check your HTML Make sure that in the page directive...

     

    I dont believe it is an IIS issue because you are getting compile errors. It cant find the overload to the method above.  What you can do and I dont have a website setup because I use web application in solution explorer go to the website top level and right click go to properties and look under the web tab. Under servers say to use visual studio development server and auto assign port.  That will take IIS out of the equation. 

     

    <%@ Page Title="" Language="C#"  AutoEventWireup="true" CodeBehind="order.aspx.cs" Inherits="Namespace.orders" %>
    
     
    Friday, July 5, 2013 8:07 AM
  • User948428231 posted

    I may have been opening the application from the wrong place. This time I opened the application from the virtual directory (local IIS), and converted it to an IIS application when prompted for it.

    It gets past the Product class error, but messes up on Line 17:

    ddlProducts.DataBind(); saying that Microsoft.Jet.OLEDB.4.0' is not registered on the local machine.

    I'm going to see what I can do about this error.

    Friday, July 5, 2013 9:09 AM
  • User948428231 posted

    I went to advanced settings on each of the application pools (I have no idea which application pool my application is using) and the error is gone. The images on the site aren't working though.

    Saturday, July 6, 2013 7:09 AM
  • User1482735129 posted
    Make sure you right click on the images and they are set to content and to copy always to bin
    Saturday, July 6, 2013 9:00 AM