0

For example I have a list called Product and it has 3 columns, ProductName (which is the Title), ProductPrice and ProductType.

  • ProductName is a string
  • ProductPrice is a currency (double)
  • ProductType is a LookUp on ProductTypes List

Normally this is easy for me if it does not contain a LookUp column, but I dont know how to deal with look up columns when Inserting.

I had tried this one but it returns an error Specified cast is not valid.

Here is the current code

EntityList<ProductTypeItem> ProductTypes = dc.GetList<ProductTypeItem>("ProductType");

ProductItem newProduct = new ProductItem();

newProduct.Title = txtProductName.Text;
newProduct.ProductPrice = double.Parse(txtProductPrice.Text); 
newProduct.ProductType = (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault();

dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();   

What would I do with the newProduct.ProductType as here is where the error occurs.

Please note that the ddProductType DataSource is the ProductType List and uses Title in its DataTextField and DataValueField

1
  • take in separete line and debug (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault(); to check if ProductTypes contains value from ddProductType Commented Mar 3, 2011 at 7:56

2 Answers 2

0

I tested your code out in LinqPad and was unable to recreate the problem; I was able to add a Product with a related ProductType to the list. Of course, since I was testing in LinqPad, I was not getting the ProductType from a DropDownList object. Instead I was getting it from a string variable. So, you might want to see what happens if you replace "ddProductType.SelectedItem.Text" with a hard-coded string

Additionally, in your subquery, if you query from "dc.ProductTypes" the first line of your code can be removed.

Also, if the Title field is unique in the ProductType list and ProductType field is required in the Product list, then I would use Single() instead of FirstOrDefault() for the following reasons: - It does a better job communicating your assumptions to someone reading the code - If these assumptions are broken, it would be easier to troubleshoot the exception if it were thrown from the subquery rather than from InsertOnSubmit() (Of course my assumptions could be wrong, so feel free to ignore this if they are)

3
  • Hi thanks for the response, i tried you suggestion on using Single() as well as hardcoding the value but its still throwing an error that says "Object reference not set to an instance of an object."
    – Raymund
    Commented Mar 3, 2011 at 3:17
  • I think you need to cast the price to a double on this line of code: newProduct.ProductPrice = txtProductPrice.Text; Commented Mar 3, 2011 at 3:58
  • Sorry that is really meant to have a double.Parse, but still it has some errors and its on the line newProduct.ProductType = (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault();
    – Raymund
    Commented Mar 3, 2011 at 4:07
0

It works now and here is the solution

EntityList<ProductTypeItem> ProductTypes = dc.GetList<ProductTypeItem>("ProductType");
Item oProductType = (from a in ProductTypes where a.Title == ddProductType.SelectedItem.Text select a).FirstOrDefault();
ProductItem newProduct = new ProductItem();

newProduct.Title = txtProductName.Text;
newProduct.ProductPrice = double.Parse(txtProductPrice.Text);
newProduct.ProductType = (ProductTypeItem)oProductType;

dc.Product.InsertOnSubmit(newProduct);
dc.SubmitChanges();   

The only thing I changed is to initialize the the Product Type as Item rather than ProductTypeItem, then cast it to ProductTypeItem now it works

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.