I am getting error when trying to import ProductTypeModel.cs file into ManagePro
ID: 3583652 • Letter: I
Question
I am getting error when trying to import ProductTypeModel.cs file into ManageProductType.aspx.cs file.
---------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProductTypeModel;
public partial class Pages_Management_ManageProductTypes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//ProductTypeModel.ProductType pt = new ProductTypeModel.ProductType();
ProductTypeModel model = new ProductTypeModel();
ProductType pt = CreateProductType();
lblResult.Text = model.InsertProductType(pt);
}
private ProductType createProductType()
{
ProductType p = new ProductType();
p.Name = txtName.Text;
return p;
}
}
ProductTypeModel.cs
---------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductTypeTypeModel
/// </summary>
namespace ProductTypeModel
{
public class ProductTypeModel
{
public string InsertProductType(ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
db.ProductTypes.Add(productType);
db.SaveChanges();
return productType.Name + "was successfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateProductType(int id, ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
// Fetch object from db
ProductType p = db.ProductTypes.Find(id);
p.Name = productType.Name;
db.SaveChanges();
return productType.Name + "was successfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string DeleteProductType(int id)
{
try
{
GarageDBEntities db = new GarageDBEntities();
ProductType productType = db.ProductTypes.Find(id);
db.ProductTypes.Attach(productType);
db.ProductTypes.Remove(productType);
db.SaveChanges();
return productType.Name + "was successfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
}
}
Explanation / Answer
You need to declare namespace in the file which class will be refernced in other files
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductTypeTypeModel
/// </summary>
namespace productTypeModelName
{
public class ProductTypeModel
{
public string InsertProductType(ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
db.ProductTypes.Add(productType);
db.SaveChanges();
return productType.Name + "was successfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateProductType(int id, ProductType productType)
{
try
{
GarageDBEntities db = new GarageDBEntities();
// Fetch object from db
ProductType p = db.ProductTypes.Find(id);
p.Name = productType.Name;
db.SaveChanges();
return productType.Name + "was successfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string DeleteProductType(int id)
{
try
{
GarageDBEntities db = new GarageDBEntities();
ProductType productType = db.ProductTypes.Find(id);
db.ProductTypes.Attach(productType);
db.ProductTypes.Remove(productType);
db.SaveChanges();
return productType.Name + "was successfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
}
}
Then you need to add this namespace in which you're referencing those classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using productTypeModelName;
public partial class Pages_Management_ManageProductTypes : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//ProductTypeModel.ProductType pt = new ProductTypeModel.ProductType();
ProductTypeModel model = new ProductTypeModel();
ProductType pt = CreateProductType();
lblResult.Text = model.InsertProductType(pt);
}
private ProductType createProductType()
{
ProductType p = new ProductType();
p.Name = txtName.Text;
return p;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.