Hello,
This is what i have come up to now this code is working now as follow
1. user type keyword in textbox
2. click on button
3. it calls to amazon and fetch data
4. bind it to label
It is only bind one record to label now i want to bind the complete list to gridview please assist.
//html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestingAmazonCode.aspx.cs" Inherits="TestingAmazonCode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="keyword" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="description" HeaderText="Price" />
<%-- <asp:BoundField ItemStyle-Width="150px" DataField="ImageURL" HeaderText="ImageURL" />
<asp:BoundField ItemStyle-Width="150px" DataField="Description" HeaderText="Description" />--%>
</Columns>
</asp:GridView>
</form>
</body>
</html>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmazonWishList.AWSService;
using System.Configuration;
using System.ServiceModel;
using AmazonWishList.Amazon.Auth;
public partial class TestingAmazonCode : System.Web.UI.Page
{
string accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
string secretKey = ConfigurationManager.AppSettings["secretKey"];
string associateTag = ConfigurationManager.AppSettings["associateTag"];
int TOTAL_PAGES = 1;
int PAGE_NUMBER = 1;
string LAST_SEARCH_KEYWORD = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GetItemDetails(string searchKeyword, bool? loadMore)
{
ItemSearchResponse response = null;
AmazonWishList.Controllers.HomeController.HomeViewModel model = new AmazonWishList.Controllers.HomeController.HomeViewModel();
model.ItemResult = new List<AmazonWishList.Controllers.HomeController.ItemSearchResult>();
if (string.IsNullOrEmpty(searchKeyword))
{
searchKeyword = LAST_SEARCH_KEYWORD;
}
else
{
LAST_SEARCH_KEYWORD = searchKeyword;
}
try
{
// send the ItemSearch request
response = GetAmazonClient.ItemSearch(this.GetItemSearch(searchKeyword, loadMore != null));
}
catch (Exception e)
{
response = null;
model.ItemResult = null;
}
if (response != null && response.Items != null && response.Items[0].Item != null)
{
if (loadMore != null)
TOTAL_PAGES = int.Parse(response.Items[0].TotalPages);
foreach (var item in response.Items[0].Item)
{
try
{
AmazonWishList.Controllers.HomeController.ItemSearchResult itemSearchRes = new AmazonWishList.Controllers.HomeController.ItemSearchResult();
itemSearchRes.ItemTitle = item.ItemAttributes.Title;
itemSearchRes.Brand = item.ItemAttributes.Brand;
itemSearchRes.ItemUrl = item.DetailPageURL;
itemSearchRes.ImageURL = item.MediumImage.URL;
itemSearchRes.Description = item.EditorialReviews != null ? item.EditorialReviews[0].Content : "";
if (item.ItemAttributes.ListPrice != null)
{
itemSearchRes.Price = item.ItemAttributes.ListPrice.FormattedPrice;
}
else if (item.OfferSummary.LowestNewPrice != null)
{
itemSearchRes.Price = item.OfferSummary.LowestNewPrice.FormattedPrice;
}
else
{
itemSearchRes.Price = "Currently unavailable";
}
model.ItemResult.Add(itemSearchRes);
//Bind one record to label i want to bind the complete list to gridview
Label1.Text = itemSearchRes.Description;
}
catch
{
// skip adding an item and continue.
}
}
}
}
private AWSECommerceServicePortTypeClient GetAmazonClient
{
get
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.MaxReceivedMessageSize = int.MaxValue;
AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
binding,
new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));
// add authentication to the client
client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));
return client;
}
}
private ItemSearch GetItemSearch(string searchKeyword, bool loadMore)
{
ItemSearch itemSearch = new ItemSearch();
itemSearch.Request = new ItemSearchRequest[] { this.GetSearchRequest(searchKeyword, loadMore) };
itemSearch.AWSAccessKeyId = accessKeyId;
itemSearch.AssociateTag = associateTag;
return itemSearch;
}
private ItemSearchRequest GetSearchRequest(string searchKeyword, bool loadMore)
{
if (loadMore && PAGE_NUMBER < 5 && (PAGE_NUMBER < TOTAL_PAGES || PAGE_NUMBER == TOTAL_PAGES))
{
PAGE_NUMBER = PAGE_NUMBER + 1;
}
else if (loadMore)
{
return null;
}
// prepare an ItemSearch request
ItemSearchRequest request = new ItemSearchRequest();
request.SearchIndex = "All";
request.Condition = Condition.All;
request.Keywords = searchKeyword;
request.ItemPage = PAGE_NUMBER.ToString();
request.ResponseGroup = new string[] { "Medium", "Reviews", "ItemAttributes", "Offers" };
return request;
}
protected void Button1_Click(object sender, EventArgs e)
{
GetItemDetails(keyword.Text, null);
}
}