C# 编译器错误使我无法将产品添加到数据库Asp.net C中#
您好,我正在创建一个Webshop网站,当我按Submit按钮向数据库添加新产品时,会出现此错误,如何更改代码,使其不会出现此错误 以下是ManageProducts.aspx.cs中的代码 这是Product.aspx.cs的代码C# 编译器错误使我无法将产品添加到数据库Asp.net C中#,c#,asp.net,C#,Asp.net,您好,我正在创建一个Webshop网站,当我按Submit按钮向数据库添加新产品时,会出现此错误,如何更改代码,使其不会出现此错误 以下是ManageProducts.aspx.cs中的代码 这是Product.aspx.cs的代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebCont
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webshop.Models;
using Webshop3.Models;
namespace Webshop3.Pages
{
public partial class Product : System.Web.UI.Page
{
public int Price { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public int TypeId { get; internal set; }
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
private void FillPage()
{
// marim te dhenat e produktit te selektuar
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
ProductModel productModel = new ProductModel();
var product = productModel.GetProduct(id);
// Mbushet faqja me data
lblPrice.Text = "Price per unit: <br/>$" + product.Price;
lblTitle.Text = product.Name;
lblDescription.Text = product.Description;
lblItemNr.Text = id.ToString();
imgProduct.ImageUrl = "~/Images/Products/" + product.Image;
// Mbushe dropdownList me numrat 1-20
int[] amount = Enumerable.Range(1, 20).ToArray();
ddlAmount.DataSource = amount;
ddlAmount.AppendDataBoundItems = true;
ddlAmount.DataBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int clientId = -1;
int id = Convert.ToInt32(Request.QueryString["id"]);
int amount = Convert.ToInt32(ddlAmount.SelectedValue);
Cart cart = new Cart
{
Amount = amount,
ClientId = clientId,
DatePurchased = DateTime.Now,
IsInCart = true,
ProductID = id
};
CartModel model = new CartModel();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用Webshop.Models;
使用Webshop3.模型;
命名空间Webshop3.Pages
{
公共部分类产品:System.Web.UI.Page
{
公共整数价格{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共字符串图像{get;set;}
public int TypeId{get;internal set;}
受保护的无效页面加载(对象发送方、事件参数e)
{
FillPage();
}
私有void FillPage()
{
//马里姆·德纳特和塞莱克图的产品
if(!String.IsNullOrWhiteSpace(Request.QueryString[“id”]))
{
int id=Convert.ToInt32(Request.QueryString[“id”]);
ProductModel ProductModel=新的ProductModel();
var product=productModel.GetProduct(id);
//Mbushet faqja me数据
lblPrice.Text=“每单位价格:
$”+产品价格;
lblTitle.Text=product.Name;
lblDescription.Text=product.Description;
lblItemNr.Text=id.ToString();
imgpproduct.ImageUrl=“~/Images/Products/”+product.Image;
//请在1-20号位置向我下线
int[]amount=Enumerable.Range(1,20).ToArray();
ddlAmount.DataSource=金额;
ddlAmount.AppendDataBoundItems=true;
ddlAmount.DataBind();
}
}
受保护的无效btnAdd_单击(对象发送者,事件参数e)
{
if(!String.IsNullOrWhiteSpace(Request.QueryString[“id”]))
{
int clientId=-1;
int id=Convert.ToInt32(Request.QueryString[“id”]);
int amount=Convert.ToInt32(ddlAmount.SelectedValue);
购物车=新购物车
{
金额=金额,
ClientId=ClientId,
DatePurchased=DateTime。现在,
IsInCart=true,
ProductID=id
};
CartModel模型=新的CartModel();
}
}
}
}
这是编译器错误
下面是ProductModel类代码
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用Webshop3.Pages;
名称空间Webshop3.Models
{
公共类产品模型
{
公共字符串插入产品(产品)
{
尝试
{
webshop2Entities1 db=新的webshop2Entities1();
db.Products.Add(产品);
db.SaveChanges();
退货产品。名称+“已成功插入”;
}
捕获(例外e)
{
返回“错误:+e;
}
}
公共字符串UpdateProduct(int-id,Product-Product)
{
尝试
{
webshop2Entities1 db=新的webshop2Entities1();
//梅雷特奥布杰克蒂酒店
productp=db.Products.Find(id);
p、 名称=产品名称;
p、 价格=产品价格;
p、 TypeId=product.TypeId;
p、 描述=产品描述;
p、 图像=产品。图像;
db.SaveChanges();
退货产品。名称+“已成功更新”;
}
捕获(例外e)
{
返回“错误:+e;
}
}
内部字符串UpdateProduct(int-id,Pages.Product)
{
抛出新的NotImplementedException();
}
公共字符串DeleteProduct(int-id)
{
尝试
{
webshop2Entities1 db=新的webshop2Entities1();
Product Product=db.Products.Find(id);
db.产品.附件(产品);
db.Products.Remove(产品);
db.SaveChanges();
退货产品。名称+“已成功删除”;
}
捕获(例外e)
{
返回“错误:+e;
}
}
公共产品GetProduct(内部id)
{
尝试
{
使用(webshop2Entities1 db=newWebshop2Entities1())
{
Product Product=db.Products.Find(id);
退货产品;
}
}
捕获(例外)
{
返回null;
}
}
公共列表GetAllProducts()
{
尝试
{
使用(webshop2Entities1 db=newWebshop2Entities1())
{
列出产品=(从数据库产品中的x开始)
选择x).ToList();
退货产品;
}
}
捕获(例外)
{
返回null;
}
}
公共列表GetProductsByType(int-typeId)
{
尝试
{
使用(webshop2Entities1 db=newWebshop2Entities1())
{
列出产品=(从数据库产品中的x开始)
其中x.TypeId==TypeId
选择x).ToList();
退货产品;
}
}
捕获(例外)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webshop.Models;
using Webshop3.Models;
namespace Webshop3.Pages
{
public partial class Product : System.Web.UI.Page
{
public int Price { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public int TypeId { get; internal set; }
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
private void FillPage()
{
// marim te dhenat e produktit te selektuar
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
ProductModel productModel = new ProductModel();
var product = productModel.GetProduct(id);
// Mbushet faqja me data
lblPrice.Text = "Price per unit: <br/>$" + product.Price;
lblTitle.Text = product.Name;
lblDescription.Text = product.Description;
lblItemNr.Text = id.ToString();
imgProduct.ImageUrl = "~/Images/Products/" + product.Image;
// Mbushe dropdownList me numrat 1-20
int[] amount = Enumerable.Range(1, 20).ToArray();
ddlAmount.DataSource = amount;
ddlAmount.AppendDataBoundItems = true;
ddlAmount.DataBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int clientId = -1;
int id = Convert.ToInt32(Request.QueryString["id"]);
int amount = Convert.ToInt32(ddlAmount.SelectedValue);
Cart cart = new Cart
{
Amount = amount,
ClientId = clientId,
DatePurchased = DateTime.Now,
IsInCart = true,
ProductID = id
};
CartModel model = new CartModel();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Webshop3.Pages;
namespace Webshop3.Models
{
public class ProductModel
{
public string InsertProduct(Product product)
{
try
{
webshop2Entities1 db = new webshop2Entities1();
db.Products.Add(product);
db.SaveChanges();
return product.Name + "was succesfully inserted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public string UpdateProduct(int id, Product product)
{
try
{
webshop2Entities1 db = new webshop2Entities1();
// Merret objekti nga db
Product p = db.Products.Find(id);
p.Name = product.Name;
p.Price = product.Price;
p.TypeId = product.TypeId;
p.Description = product.Description;
p.Image = product.Image;
db.SaveChanges();
return product.Name + "was succesfully updated";
}
catch (Exception e)
{
return "Error:" + e;
}
}
internal string UpdateProduct(int id, Pages.Product product)
{
throw new NotImplementedException();
}
public string DeleteProduct(int id)
{
try
{
webshop2Entities1 db = new webshop2Entities1();
Product product = db.Products.Find(id);
db.Products.Attach(product);
db.Products.Remove(product);
db.SaveChanges();
return product.Name + "was succesfully deleted";
}
catch (Exception e)
{
return "Error:" + e;
}
}
public Product GetProduct(int id)
{
try
{
using (webshop2Entities1 db = new webshop2Entities1())
{
Product product = db.Products.Find(id);
return product;
}
}
catch (Exception)
{
return null;
}
}
public List<Product> GetAllProducts()
{
try
{
using (webshop2Entities1 db = new webshop2Entities1())
{
List<Product> products = (from x in db.Products
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
public List<Product> GetProductsByType(int typeId)
{
try
{
using (webshop2Entities1 db = new webshop2Entities1())
{
List<Product> products = (from x in db.Products
where x.TypeId == typeId
select x).ToList();
return products;
}
}
catch (Exception)
{
return null;
}
}
}
}