Use Amazon Web Services in ASP.NET

Anyone manage to get it working in Visual Web Developer beta yet?

I managed to get it working on Dot.Net 1.1 back in November but gave up on 2.0 beta. I don’t have VS2005 yet.

Andrew

Can anybody help me to add the prices? Just can’t get it.

steve

I don’t know who fixed what but the sample now works using the February CTP of Visual Web Developer and ECS 4.0. (for the US anyway, havn’t tried any other local)

http://lab.msdn.microsoft.com/express/vwd/default.aspx
http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

I would like to do some advanced searches, and incorporate the cart system. Is there anybody that could help me please? I tried the Amazon discussion site for the last weeks, but I can’t get an answer.

Thank you,

Steve

hi,

Ive been going round and round in circles, Ive had it working, then not after trying some variations. I have now gone back to the original tutorial and I cannot get the simple part to even work. I have tried to get it to work in VWD 2005, but it will not pickup the web reference (e.g. using appname.com.amazon.webservices) and so wont compile as it has trouble recognising the name space and the WSDL (http://webservices.amazon.com/AWSECommerceService/UK/AWSECommerceService.wsdl)
.
I have now gone back to using VS2002 with dot net 1.1. Everything seems to work fine and it compiles, but when I run it in the browser I get this error:

System.NullReferenceException: Object reference not set to an instance of an object. at AmazonVSO.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\amazonvso\webform1.aspx.cs:line 75

This error relates to the for loop for iterating through the result set, but for some reason it doesnt like the “i < items.length” part
Here’s the full source, would anyone be able to help me out and suggest what is going on please?? Thanks in advance.

Source:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AmazonVSO.com.amazon.webservices;

namespace AmazonVSO
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;

	private void Page_Load(object sender, System.EventArgs e)
	{
		// Put user code to initialize the page here
	}

	#region Web Form Designer generated code
	override protected void OnInit(EventArgs e)
	{
		//
		// CODEGEN: This call is required by the ASP.NET Web Form Designer.
		//
		InitializeComponent();
		base.OnInit(e);
	}
	
	/// &lt;summary&gt;
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// &lt;/summary&gt;
	private void InitializeComponent()
	{    
		this.Button1.Click += new System.EventHandler(this.Button1_Click);
		this.Load += new System.EventHandler(this.Page_Load);

	}
	#endregion

	private void Button1_Click(object sender, System.EventArgs e)
	{
		AWSECommerceService aws = new AWSECommerceService();
		ItemSearchRequest request = new ItemSearchRequest();

		request.SearchIndex = "Books";
		request.Power = "title:" + TextBox1.Text;
		request.ResponseGroup = new string[] { "Small" };
		request.Sort = "salesrank";

		ItemSearchRequest[] requests = new ItemSearchRequest[] { request };

		ItemSearch itemSearch = new ItemSearch();
		itemSearch.AssociateTag = "Associate tag";
		itemSearch.SubscriptionId = "XXXXXXXXXXXXXXXXX";
		itemSearch.Request = requests;

		try
		{
			ItemSearchResponse response = aws.ItemSearch(itemSearch);
			Items info = response.Items[0];
			Item[] items = info.Item;
			Label1.Text = "";
			
			for (int i = 0; i &lt;items.Length; i++)
					{
						Item item = items[i];
						Label1.Text += "Book Title: " + item.ItemAttributes.Title + "&lt;br /&gt;";
					}
		}
		catch (Exception ex)
		{
			Label1.Text = (ex.ToString());
		}
	
	}
}

}

// shoul be
using com_amazon_webservices

You get NullReference Exception probably because you didn’t replace XXXXXXXXXXXXXXXXX with your amazon SubscriptionID which you get when you subscribe with Amazon web services.

itemSearch.SubscriptionId = “XXXXXXXXXXXXXXXXX”; -> replace with your unique ID

Actually, that is not so now days since Subscription ID Is only required for those who signed up before 10/2005. For us (like me) who signed on after, we do not get assigned a Subscription ID. We only have the AWSAccessKeyID.

Can reach me @ shannara@unformed.com but as it stands, this code does not work with the newest Amazon service and with vs2005.

AWS works perfectly in VS2005. With Access Key ID supplied as subscription #.

m.ash@sify.com

When I type in the first part of the code or copy and paste it I get the following error:

c:\Inetpub\wwwroot\Amazontest\Default.aspx.cs(21,7): error CS1003: Syntax error, ‘(’ expected
c:\Inetpub\wwwroot\Amazontest\Default.aspx.cs(23,21): error CS1026: ) expected
c:\Inetpub\wwwroot\Amazontest\Default.aspx(12,59): error CS0117: ‘ASP.default_aspx’ does not contain a definition for ‘Button1_Click1’

Any ideas why?

I have a question. If i change the “Books” to “DVD” it wont work. Any ideas why? Only message i get is Object reference not set to an instance of an object

Could anybody help me troubleshoot?
http://www.alanhostudio.com/maddie1/amazon.aspx

I’ve added the web reference:
http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

amazon.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="amazon.aspx.cs" Inherits="amazon" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        
    </div>
    </form>
</body>
</html>

amazon.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.amazon.webservices;

public partial class amazon : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        AWSECommerceService aws = new AWSECommerceService();
        ItemSearchRequest request = new ItemSearchRequest();

        request.SearchIndex = "Books";
        request.Power = "title:" + TextBox1.Text;
        request.ResponseGroup = new string[] { "Small" };
        request.Sort = "salesrank";

        ItemSearchRequest[] requests = new ItemSearchRequest[] { request };

        ItemSearch itemSearch = new ItemSearch();
        itemSearch.AssociateTag = "***-20"; //took out for forum
        itemSearch.AWSAccessKeyId = "***"; //took out for forum
        itemSearch.Request = requests;

        try
        {
            ItemSearchResponse response = aws.ItemSearch(itemSearch);
            Items info = response.Items[0];
            Item[] items = info.Item;
            Label1.Text = "";
            for (int i = 0; i < items.Length; i++)
            {
                Item item = items[i];
                Label1.Text += "Book Title: " + item.ItemAttributes.Title + "<br />";
            }
        }
        catch (Exception ex)
        {
            Label1.Text = (ex.ToString());
        }
    }
    public DataSet AmazonToDataSet(string PowerSearchString)
    {
        DataSet ds;

        //have we this in the cache already?  
        if (Cache[PowerSearchString] == null)
        {
            //create our query  
            ItemSearchResponse response =
                grabAmazonSearch(PowerSearchString);

            //Construct the dataset to store the results  
            ds = new DataSet();
            ds.Tables.Add();

            ds.Tables[0].Columns.Add("ProductName");
            ds.Tables[0].Columns.Add("AuthorName");
            ds.Tables[0].Columns.Add("Price");
            ds.Tables[0].Columns.Add("URL");

            Items info = response.Items[0];
            Item[] items = info.Item;
            DataRow dr;

            //iterate the results adding new rows with  
            //the values from the search result item.  

            for (int i = 0; i < items.Length; i++)
            {
                Item item = items[i];
                dr = ds.Tables[0].NewRow();
                dr["ProductName"] = item.ItemAttributes.Title;
                dr["AuthorName"] = item.ItemAttributes.Author[0];
                dr["Price"] = item.ItemAttributes.ListPrice;
                dr["URL"] = item.DetailPageURL;
                ds.Tables[0].Rows.Add(dr);
            }

            //add the dataset to the cache  
            Cache.Insert(PowerSearchString, ds, null,
                DateTime.Now.AddHours(24), TimeSpan.Zero);

        }
        else
        {
            ds = (DataSet)Cache[PowerSearchString];
        }
        //return the dataset  
        return ds;
    }

    private ItemSearchResponse grabAmazonSearch(string PowerSearchString)
    {
        ItemSearchResponse response;
        AWSECommerceService aws = new AWSECommerceService();
        ItemSearchRequest request = new ItemSearchRequest();

        request.SearchIndex = "Books";

        request.Power = PowerSearchString;
        request.ResponseGroup = new string[] { "Small" };
        request.Sort = "salesrank";

        ItemSearchRequest[] requests = new ItemSearchRequest[] { request };

        ItemSearch itemSearch = new ItemSearch();
        itemSearch.AssociateTag = "***-20"; //took out for forum
        itemSearch.AWSAccessKeyId = "***"; //took out for forum
        itemSearch.Request = requests;

        response = aws.ItemSearch(itemSearch);

        return response;
    }

}

The using directive needs to go to the top of the page, and it is named wrong. It should be: using com.amazon.webservices;

This article really interested me, but I need it in VB rather than C#. Would anyone be able to convert it for me? My VB skills are not very good, but this is the chosen platform I must use.

Thanks for your time
Sniff

This might be of some use: http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

If you have any problems, just post them in a new thread.

Brilliant. Thanks. If I have time I’ll let you know how I get on.

Thanks again.
Sniff

Great!! Thanks a lot!

I’m stuck at the first step. When I paste the code into the event handler, visual studio can’t compile the code. It gets stuck at the first line, using com_webservices_amazon, saying ‘Syntax error, ‘(’ expected’. Any suggestions?

I’ve fixed this now. The code was used in an aspx file so, you need to add
<%@ Import Namespace=“com.amazon.webservices” %>
to the top of the page (in case anyone else has the same problem)

I’m using VWD Express 2005 and got it working. I was wondering if anyone knew why the ListPrice doesn’t display, and how to fix it? Also, how would I make a picture of the book appear? Any ideas? Thanks in advance.