Within Sharepoint, I have a Products list and a Categories list. I'm trying to create a new (Product) item from a web form. The Product list has a Category lookup column, and when i try to create a new instance of Product, I get a null reference error on my insert. I read a post that the item needs to be created with the non-lookup column values, retrieve the new item's id, then insert the value into the lookup column, but I get the same error. Can anyone help with the correct way to do this?
ProductDevelopmentDataContext dc = new ProductDevelopmentDataContext(SPContext.Current.Web.Url);
EntityList<ProductItem> Product = dc.GetList<ProductItem>("Product");
ProductItem newProduct = new ProductItem();
newProduct.Title = title.Text;
newProduct.ProductDescription = description.Text;
dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();
var newProductID = (int)newProduct.Id;
ProductDevelopmentDataContext newdc = new ProductDevelopmentDataContext(SPContext.Current.Web.Url);
newProduct = (from p
in newdc.Product
where p.Id == newProductID
select newProduct).Single();
newProduct.CategoryName.Title = category.Text;
newdc.Product.InsertOnSubmit(newProduct);
newdc.SubmitChanges();