DbContext.SaveChanges Method Throw null reference exception while saving data

asp.net c# entity-framework-6

Question

I use EF 6.0 for my web application, and it always throws a "null reference exception" when I try to save or update data via EF. This process is in charge of producing goods.

private Product createProduct()
{
    Product product = new Product();
    product.Name = txtName.Text;
    product.Price = Convert.ToInt32(txtPrice.Text);
    product.ProductTypeId = Convert.ToInt32(ddlType.SelectedValue);
    product.Desciption = txtDesciption.Text;
    product.Image = ddlImage.SelectedValue;
    return product;
}

"Insert Product Method" is being called by this method.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ProductModel model = new ProductModel();
    Product product = createProduct();
    lblStatus.Text = model.InsertProduct(product);
}

This method throws an exception while conducting the "product insertion function."

public string InsertProduct(Product product)
{
    try
    {
        PizzaHuttSliceEntities db = new PizzaHuttSliceEntities();
        db.Products.Add(product);
        db.SaveChanges();
        return product.Name + "was successfully inserted! ";
    }
    catch (Exception e)
    {
        return "Error: " + e;
    }
}

This is a product class.

public partial class Product
{
    public Product()
    {
        this.Carts = new HashSet<Cart>();
    }

    public int ID { get; set; }
    public int ProductTypeId { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
    public string Desciption { get; set; }
    public string Image { get; set; }

    public virtual ICollection<Cart> Carts { get; set; }
    public virtual ProductType ProductType { get; set; }
}

Here is the tracking information

enter image description here

1
1
4/19/2016 5:34:09 PM

Popular Answer

ZZZ_tmp
1
4/20/2016 4:20:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow