Friday, May 16, 2008

Insert data from Amazon new Webservice to access database by node ID

Insert data from Amazon Webservice to access database by node ID after that you can display these data on your webpage, I used C# , asp.net 2.0 , visual stdios 2005 , access , and new amazon webservice .


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.OleDb;

public partial class svuploaddata : System.Web.UI.Page
{
public static string SalesRankOut = "";
public static string DetailPageURLOut = "";
public static string imgURLOut = "";
public static string FeatureOut = "";
public static string TitleOut = "";
public static string FormattedPriceOut = "";


protected void Page_Load(object sender, EventArgs e)
{
//detele existing data
deletdatDB();
//Define node by pageno
getNodeItems("551242", "1");
getNodeItems("551242", "2");
getNodeItems("289913","1");
Response.Write("
"+"Process completed"+"
");
}
void getNodeItems(String Nodeno, String ItemPageNo)
{
//get node items
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&AssociateTag=wwwvislicom-20&AWSAccessKeyId=16ND0GSSVYMWG9M8ECG2&Operation=ItemSearch&SearchIndex=HomeGarden&BrowseNode=" + Nodeno + "&Version=2006-06-07&ItemPage=" + ItemPageNo + "&ResponseGroup=SalesRank&Sort=salesrank");
// Response.Write("http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&AssociateTag=wwwvislicom-20&AWSAccessKeyId=16ND0GSSVYMWG9M8ECG2&Operation=ItemSearch&SearchIndex=HomeGarden&BrowseNode=" + Nodeno + "&Version=2006-06-07&ItemPage=" + ItemPageNo + "&ResponseGroup=SalesRank&Sort=salesrank");
XmlNodeList Name = xDoc.GetElementsByTagName("ASIN");
int cols = Name.Count;
for (int i = 0; i <= cols - 1; i++)
{
string Xname = Name[i].InnerText;
getItemDetails(Xname);
}
}

//get item deatils..
void getItemDetails(String Itemno)
{
XmlDocument doc = new XmlDocument();
doc.Load("http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=xxxxxxxxxxxxxxxxxxxx&Operation=ItemLookup&IdType=ASIN&AssociateTag=wwwvislicom-20&ResponseGroup=Medium&ItemId=" + Itemno);
//Response.Write("http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=xxxxxxxxxxxxxxxxxxxx&Operation=ItemLookup&IdType=ASIN&AssociateTag=wwwvislicom-20&ResponseGroup=Medium&ItemId=" + Itemno);
XmlNodeList prodList = doc.GetElementsByTagName("Item");

//SalesRank
{
XmlNodeList SalesRank = doc.GetElementsByTagName("SalesRank");
if (((SalesRank.Count - 1) >= 0))
{
SalesRankOut = SalesRank[0].InnerXml;
}
}

//Title
{
XmlNodeList DetailPageURL = doc.GetElementsByTagName("Title");
if (((DetailPageURL.Count - 1) >= 0))
{
TitleOut = DetailPageURL[0].InnerXml;
}
}
//FormattedPrice
{
XmlNodeList DetailPageURL = doc.GetElementsByTagName("FormattedPrice");
if (((DetailPageURL.Count - 1) >= 0))
{
FormattedPriceOut = DetailPageURL[0].InnerXml;
}
}

//DetailPageURL
{
XmlNodeList DetailPageURL = doc.GetElementsByTagName("DetailPageURL");
if (((DetailPageURL.Count - 1) >= 0))
{
DetailPageURLOut = DetailPageURL[0].InnerXml;
}
}

//image SmallImage
XmlNodeList imgURL = doc.GetElementsByTagName("URL");

if (((imgURL.Count - 1) >= 0))
{
imgURLOut = imgURL[5].InnerXml;
}

//features
int i = 0;
string fFeature1;
string fFeature;
XmlNodeList elemList3 = doc.GetElementsByTagName("Feature");
int tfeature = (elemList3.Count - 1);

if (((elemList3.Count - 1) <= 0))
{
fFeature1 = "";
}
else
{
fFeature1 = ("

  • " + elemList3[0].InnerXml);
    for (i = 1; (i <= (elemList3.Count - 1)); i++)
    {
    fFeature1 = (fFeature1 + ("
  • " + elemList3[i].InnerXml));
    FeatureOut = fFeature1;

    }

    }
    /*
    Response.Write(SalesRankOut);
    Response.Write("
    ");
    Response.Write(DetailPageURLOut);
    Response.Write("
    ");
    Response.Write(imgURLOut);
    Response.Write("
    ");
    //Response.Write(FeatureOut);
    Response.Write("
    ");
    Response.Write(TitleOut);
    Response.Write("
    ");
    Response.Write(FormattedPriceOut);
    Response.Write("
    ");
    */
    insertdata();

    }


    void insertdata()
    {

    string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\sites\\Single15\\xxxx\\database\\xxxxxxx.mdb";
    OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
    OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

    string ProdTy = "M2";
    System.DateTime today = System.DateTime.Now;
    string prodesc = "Sale Price: " + FormattedPriceOut + "
    Product Features
    " + FeatureOut;
    string prodescout = prodesc.Replace("'", "" + (char)146);
    string company = "Amazon";
    string fut1 = "2";


    string ProdDesc;
    //string Strsql = "INSERT INTO DetailUrl(NameURL, TextURL,ImageURL,ProdDesc,ComName,ProdType,Price,UpdDate,fut2,fut1)VALUES ('" + DetailPageURLOut + "','" + TitleOut + "','" + imgURLOut + "'," + @ProdDesc + ",'" + company + "','" + ProdTy + "','" + FormattedPriceOut + "','" + today + "','" + fut2 + "','" + fut1 + "' )";
    string Strsql = "INSERT INTO svprodtab(NameURL, TextURL,ImageURL,ProdDesc,ComName,ProdType,Price,UpdDate,fut2,fut1)VALUES (@NameURL, @TextURL,@ImageURL,@ProdDesc,@ComName,@ProdType,@Price,@UpdDate,@fut2,@fut1)";
    OleDbConnection MyConn = new OleDbConnection(connectionString);
    myOleDbCommand.CommandText = Strsql;
    OleDbCommand cmd = new OleDbCommand(Strsql, MyConn);
    cmd.Parameters.AddWithValue("@NameURL", DetailPageURLOut);
    cmd.Parameters.AddWithValue("@TextURL", TitleOut);
    cmd.Parameters.AddWithValue("@ImageURL", imgURLOut);
    cmd.Parameters.AddWithValue("@ProdDesc", prodesc);
    cmd.Parameters.AddWithValue("@ComName", company);
    cmd.Parameters.AddWithValue("@ProdType", ProdTy);
    cmd.Parameters.AddWithValue("@Price", FormattedPriceOut);
    cmd.Parameters.AddWithValue("@UpdDate", System.DateTime.Now);
    cmd.Parameters.AddWithValue("@fut2", SalesRankOut);
    cmd.Parameters.AddWithValue("@fut1", fut1);
    MyConn.Open();
    cmd.ExecuteNonQuery();
    MyConn.Close();



    }

    void deletdatDB()
    {
    string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\sites\\Single15\\xxxxxx\\database\\xxxxxx.mdb";
    OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
    OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
    string Strsql = "delete from svprodtab";
    OleDbConnection MyConn = new OleDbConnection(connectionString);
    myOleDbCommand.CommandText = Strsql;
    OleDbCommand cmd = new OleDbCommand(Strsql, MyConn);
    MyConn.Open();
    cmd.ExecuteNonQuery();
    MyConn.Close();
    Response.Write("Data Deleted" + "
    ");
    }


    }


    www.svdeals.com



  • No comments: