Page 1 of 1
Any example code for simple data access?
Posted: Fri May 30, 2008 4:49 am
by moopa
Hi.
We are currently using the demo of ablecommerce to see if we can customize it for our needs.
To get (almost) to the point, most of the actual front end is irrelevant for us and i just want to use products database and load single products into flash using a convenient method such as xml. I have downloaded the API docs but on their own they are a little daunting. Can anyone point me in the direction of a simple example getting a single product from the database using the api? It will be a great help to me!
Thank you very much.
Dave
Re: Any example code for simple data access?
Posted: Fri May 30, 2008 5:05 am
by m_plugables
Following code will load a product for some product id
Code: Select all
Product product = ProductDataSource.Load(productid);
Following code will load product or products based upon some SQL criteria
Code: Select all
ProductCollection products = ProductDataSource.LoadForCriteria("some sql criteria like Name='abx' ");
You can found more information on this thread.
viewtopic.php?f=42&t=7297
Re: Any example code for simple data access?
Posted: Fri May 30, 2008 6:33 am
by moopa
mazhar: Thanks very much for the post. I was tinkering and had worked out getting a product and i am pleased to say i did it in the same way you have!
I wrote a little test code and thought it may help other people who have just installed ablecommerce (below).
Code: Select all
protected void Page_Load(object sender, EventArgs e)
{
int ProductId = 1; // Test product id
// Load a product
Product p = CommerceBuilder.Products.ProductDataSource.Load(ProductId);
StringBuilder sb = new StringBuilder();
sb.Append("Product Name: " + p.Name + "<br />");
sb.Append("Price: " + p.Price + "<br />");
sb.Append("Product Image: " + p.ImageUrl + "<br />");
// Display product options (for example color or size).
ProductOptionCollection poCollection = p.ProductOptions;
foreach (ProductOption po in poCollection)
{
// Display each of the option choices (Small, Medium Large).
OptionChoiceCollection ocCollection = po.Option.Choices;
sb.Append("<br />Option: " + po.Option.Name + "<br /><br />");
foreach(OptionChoice oc in ocCollection)
{
sb.Append("Choice: " + oc.Name.ToString() + "<br />");
}
}
Response.Write(sb.ToString());
}
Many thanks
Dave
Re: Any example code for simple data access?
Posted: Fri May 30, 2008 6:51 am
by m_plugables
Thanks for sharing your experience, these sort of things can help out many people.