Developer forum
E-mail notifications
Create product programatically
I want to create a product programatically in an .aspx page.
Currently I don't get any errors when calling the Save() method on the product object, but nothing is saved in the database.
I set the following properties on my product object:
1) Name
2) Active (setting it to true).
4) DefaultShopID (setting it to "Dynamicweb.eCommerce.Shops.Shop.getShops("SHOP1").get_Item(0).ID;")
5) Language (setting it to "Dynamicweb.eCommerce.Common.Application.DefaultLanguage;")
I also add a Group to the Product.Groups GroupCollection (Group with ID GROUP1).
As mentioned earlier, I don't get any errors when calling the Product.Save() method, but nothing is saved in the database. When debugging I see that the DefaultShopID and Group is set correctly (The shop with id SHOP1 actually exists and the Group with ID GROUP1 exists as well).
Any help would be much appriciated.
Thanks in advance
René
Replies
Hi René
Unfortunately you don't get any errors because the exception is trapped. The reason you don't see your product saved is because you probably didn't set the Created and Modified properties. Just set them to DateTime.Now.
Hope this helps :)
- Jeppe
Thanks for the quick reply :)
Unfortunately settings the Created and Updated (no property is called Modified so i guess you mean the Updated property) doesn't do the trick. Still I get no error and nothing is saved.
I think it's a bit weird that the exception is trapped - it's ok that i can get errors in my code. Then, off course, it would be up to me to trap the exceptions, but that's a better way for me as a developer :)
Forgot to set the ID of the product to "" (empty string).
Did like this before: Product p = new Product();
But have to do like this: Product p = new Product("");
Doing this it is working like it's supposed to. I'm also setting the two dates, but I don't know if you actually have to do it for it to work.
I wrote the first post on the train off the top of my head, so yes, I was thinking of Updated :)
I'm glade you found the issue.
Also you are correct. Exceptions should not be trapped in propriatary code except in some special circumstances. We are planning to fix this and a lot of other inconveniences still left over in the current version in 8.0.
- Jeppe
Hi,
Hope these two posts will helpful for you:
http://developer.dynamicweb-cms.com/Forum/Development/Custom-module---product-import-with-variants.aspx
http://developer.dynamicweb-cms.com/forum/development/removing-a-product-from-a-group.aspx?PID=48
Kind Regards
Zhukovskiy Yury
You must be logged in to post in the forum