Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hai,


I am having online shopping application.I have 3 classes class1,class2,captchaimage.I have to create object for this classes into my backend code.I tried to create object.But the error
"The type or namespace name 'Class1' could not be found (are you missing a using directive or an assembly reference?)" is coming.How can I solve this.Below is a code behind file.Please help me

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    int flag = 0;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        bind_cart();
       // Response.Write(Session["category"]);
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {

    }

    protected void DataList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton lbu = (LinkButton)e.CommandSource;      
        Response.Redirect("products.aspx?catname=" + lbu.Text);       
    }

    public void bind_cart()
    {
        Class1 ob = new Class1();----->I am getting error in this line
        ob.dt = (DataTable)Session["cart"];
        string str = "";

        if (ob.dt.Rows.Count == 0)
        {
            str = "_______________________________";

            ListBox1.Items.Add(str);
            str = "No Item Selected";
            ListBox1.Items.Add(str);
            str = "_______________________________";
            ListBox1.Items.Add(str);
        }
        else
        {
            str = "    " + "Product  " + "Quantity";
            ListBox1.Items.Add(str);
            str = "_______________________________";
            ListBox1.Items.Add(str);
            int index = 1;

            for (int j = 0; j <= ob.dt.Rows.Count - 1; j++)
            {
                DataRow dr = ob.dt.Rows[j];
                str = Convert.ToString(index) + ". " + Convert.ToString(dr["pname"]) + "  " + Convert.ToString(dr["qty"]);
                ListBox1.Items.Add(str);
                index++;

            }
            int total = Class2.gettotalprice();---->Getting error this line

            str = "_______________________________";
            ListBox1.Items.Add(str);
            str = "Total Amount=  " + total.ToString();

            ListBox1.Items.Add(str);
        }

    }
    
}
Posted

If your class has a different namespace, then this would not work. What you need to do is to reference the namespace of your class on your codebehind. Consider the following example.

C#
using MyApp.Objects1; //take note that I referenced the namespace in order to have access to Class1

namespace MyApp.Objects2
{
   public class Class2
   {
      private Class1 _myClass1;
      public Class2()
      {
         _myClass1 = new Class1(); 
      }
   }
}

//Assuming this is the structure of Class1
namespace MyApp.Objects1 //take note of the namespace
{
   public class Class1
   {
      public Class1(){ }
   }
}
 
Share this answer
 
If Class1 is in another assembly, you will first need to add reference to that in your web application. If it is in another namespace, add a "using" statement for those namespaces.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900