1

Am using EF 6.0 for my web application, whenever i tried to save or update data via EF, it throws a "null reference exception".. this method is responsible for creating products

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;
}

this method is calling "insert product method"

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

This method is performing "product insertion function" and it throws exception

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;
    }
}

Here is 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 Trace Info

enter image description here

13
  • 1
    Did you debug to see where your null reference is occurring? Did you google "NullReferenceException" and the common causes of such an error? Commented Apr 19, 2016 at 15:38
  • yup,,, i've tried too many times but can't find the source of this exception.. in debugging.... the exception occurs at db.savechanges() in "insertProduct method" Commented Apr 19, 2016 at 15:44
  • when i tried at database directly,,, everything works fine but the problem occurs at application... i've checked too many times but can't find null reference Commented Apr 19, 2016 at 15:49
  • Is PizzaHuttSliceEntities a dbcontext class? ie Does it extend DbContext Commented Apr 19, 2016 at 15:49
  • 1
    In my experience, exceptions thrown by the SaveChanges method usually are (or contain) an exception of type System.Data.Entity.Validation.DbEntityValidationException. That exception type has a property called EntityValidationErrors which contains more information about what happened. I'd look at the exception in debugger and see if you can find a DbEntityValidationException buried in there. Commented Apr 19, 2016 at 18:05

1 Answer 1

1

Solved! Actually the problem is with ddlType DataSource, i just refresh the schema of SqlDataSource for ddlType and the problem vanished. Because i did an update to the database and that's why the ddlType pointing to such reference which wasn't exist anymore and for this reason DbContext.SaveChanges method throwing null reference exception Thanks to all who pay attention to my question

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.