Example #1
0
        private void GetInfoFromISBN(string isbn, string technology)
        {
            if (isbn.Length != 10)
            {
                return;
            }


            AWSProductData productData = new AWSProductData();
            ItemLookup     lookup      = null;

            try
            {
                ItemLookupRequest req = new ItemLookupRequest();
                req.IdType    = ItemLookupRequestIdType.ASIN;
                req.ItemId    = new string[1];
                req.ItemId[0] = isbn;
                // req.SearchIndex = "Books";

                lookup = new ItemLookup();
                lookup.AssociateTag   = "libertyassocia00A";
                lookup.SubscriptionId = "0SD959SZV6KXV3BKE2R2";
                lookup.Request        = new ItemLookupRequest[1];
                lookup.Request[0]     = req;
            }
            catch (System.Exception e)
            {
                lblStatus.Text = e.Message;
            }


            ItemLookupResponse response;
            Items info;

            Item[] items;
            Item   item;



            int    salesRank = -1;
            string author    = string.Empty;
            string pubDate   = string.Empty;
            string publisher = string.Empty;
            string title     = string.Empty;
            string strURL    = string.Empty;

            try
            {
                response  = productData.ItemLookup(lookup);
                info      = response.Items[0];
                items     = info.Item;
                item      = items[0];
                salesRank = item.SalesRank == null ? -1 : Convert.ToInt32(item.SalesRank);
                author    = FixQuotes(item.ItemAttributes.Author[0]);
                pubDate   = FixQuotes(item.ItemAttributes.PublicationDate);
                publisher = FixQuotes(item.ItemAttributes.Publisher);
                title     = FixQuotes(item.ItemAttributes.Title);
                strURL    = item.DetailPageURL;
            }
            catch (System.Exception ex)
            {
                lblStatus.Text = ex.Message;
            }

            // update the list box
            string results = title + " by " + author + ": " +
                             publisher + ", " + pubDate + ". Rank: " + salesRank;

            lbOutput.Items.Add(results);
            lbOutput.SelectedIndex = lbOutput.Items.Count - 1;

            // update the database
            string commandString = @"Update BookInfo set isbn = '" +
                                   isbn + "', title = '" + title + "', publisher = '" +
                                   publisher + "', pubDate = '" + pubDate + "', rank = " +
                                   salesRank + ", link = '" + strURL + "', lastUpdate = '" +
                                   System.DateTime.Now + "', technology = '" +
                                   technology + "', author = '" +
                                   author + "' where isbn = '" +
                                   isbn + "'";

            command.CommandText = commandString;
            try
            {
                // if no rows were affected, this is a new record
                connection.Open();
                int numRowsAffected = command.ExecuteNonQuery();
                if (numRowsAffected == 0)
                {
                    commandString = @"Insert into BookInfo values ('" +
                                    isbn + "', '" + title + "', '" + publisher + "', '" +
                                    pubDate + "', '" + FixQuotes(strURL) + "', " + salesRank + ", '" +
                                    System.DateTime.Now +
                                    "', '" + technology + "', '" + author + "')";

                    command.CommandText = commandString;
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
                lbOutput.Items.Add("Unable to update database!");
                lbOutput.SelectedIndex = lbOutput.Items.Count - 1;
            }
            finally
            {
                connection.Close(); // clean up
            }
        }                           // close for GetInfoFromISBN