Returning Item Detail
That completes the first portion of the application. The second requirement is to get the detail information when a user clicks one of the titles. To do that, use the ListBox's SelectedIndexChanged event to retrieve the ASIN number of the selected item, and then perform an AsinSearchRequest to get the detail data.
// create a new AsinRequest object
ar = new AsinRequest();
// get the ASIN number from the Details item stored
// in the ListBox
ar.asin = ((Details) listBox1.SelectedItem).Asin;
// set properties
ar.tag = "webservices-20";
// perform the search
pi = amazonSrch.AsinSearchRequest(ar);
Details allDetails = pi.Details;
// code to display detail here
This time, you want to perform a "heavy" search to return all the details. After retrieving the details, the sample code displays a selected subset by concatenating the information using a StringWriter, and then assigning it to the txtDetails TextBox on the form.
StringWriter sw = new StringWriter();
StringBuilder sb = new StringBuilder();
if ((allDetails != null) && (allDetails.Length > 0))
Details d = allDetails;
sw.WriteLine("Title: " + d.ProductName );
sw.WriteLine("Asin: " + d.Asin);
String authors = d.Authors;
if (authors != null)
if (authors.Length > 1)
for (int i=0; i < authors.Length; i++)
sw.WriteLine("Availability: " + d.Availability);
sw.WriteLine("Browselist: " + d.BrowseList);
sw.WriteLine("Catalog: " + d.Catalog);
sw.WriteLine("CollectibleCount: " + d.CollectibleCount);
sw.WriteLine("CollectibelPrice: " + d.CollectiblePrice);
sw.WriteLine("Distributor: " + d.Distributor);
sw.WriteLine("ImageUrlSmall: " + d.ImageUrlSmall);
sw.WriteLine("ImageUrlMedium: " + d.ImageUrlMedium);
sw.WriteLine("ImageUrlLarge: " + d.ImageUrlLarge);
this.txtDetail.Text = sw.ToString();
// more code here
Displaying Images from a URL
The final task is to display the image. The ASIN request returns image URLs for large, medium, and small images, when available. The best size for the sample application is the medium image. Unfortunately, there's no simple method call to retrieve an image from a URL. Instead, you need to make a separate request to get the image data. The ImageFromURL() method in the sample application solves that problem.
// add a using System.Net reference to your class
// retrieve an image from a URL
private Image ImageFromUrl(String url)
WebClient wc = new WebClient();
Stream st = wc.OpenRead(url);
Image im = Image.FromStream(st);
The ImageFromUrl method creates a WebClient instance and uses it to request the image data from the specified URL, assigns that to a Stream, and then loads the Image from the stream and returns it. You can assign the returned image directly to the PictureBox.Image property (see Figure 2):
// the variable d is the instance of the Details class filled with data returned
// from the AsinSearchRequest method.
this.pictureBox1.Image = this.ImageFromUrl(d.ImageUrlMedium);
| Author's Note: The application only displays the medium-sized image, when it's available. Not all books have associated imagesand of those that do, the image may not be available. Missing images don't cause an error, they simply don't display.
This sample application is probably enough to get you started, but you can go far beyond this simple example and write applications that notify users when new books become available, monitor the sales rank of items on Amazon, retrieve Wish Lists and Marketplace information, and add transactional purchasing, including Amazon's "Quick-Click Buying" capability to your applications and Web pages.
You should be aware that there are some limitations attached to these free developer services. For example, as a developer, you're not allowed to make queries more than once per second, nor make queries containing more than 20K of data (which seems eminently reasonable). You agree to refresh cached information periodically; the refresh interval depends on the type of information. Read the Amazon.com_License.txt file carefully before you decide to use the services in your applications.