Link to home
Start Free TrialLog in
Avatar of asif07
asif07

asked on

c# array datasets

I am scraping the site and i want to save the price and name in different arrays, so i can display them on my site.  I am a little stuck.  how do i add the two elements of each rows to arrays?   Is there a better way?
Here is my code:
[WebMethod]
        public DataSet  getKeyWordWS(string keywords) {
            StreamReader oSR = null;
            string strURL = "http://www.amazon.com/s/ref=nb_ss_gw/104-5509892-3266348?url=search-alias%3Daps&field-keywords=" + keywords + "&x=0&y=0";
            WebRequest objRequest = WebRequest.Create(strURL);
            WebResponse objResponse = objRequest.GetResponse();
            oSR =  new StreamReader(objResponse.GetResponseStream());
            string strContent = oSR.ReadToEnd();
 
            DataSet data = new DataSet();
            DataTable dataTable = data.Tables.Add();
            dataTable.Columns.Add("Name", typeof(string));
            dataTable.Columns.Add("Price", typeof(string));
 
            List<string> productNameArray = new List<string>(20);
            List<string> productPriceArray = new List<string>(20);
            
 
            
            
            Regex regex = new Regex("<div id=\"Results\"((.|\n)*?)</div>", 
                RegexOptions.IgnoreCase);
            Match oM = regex.Match(strContent);
          
            Regex keyRegex = new Regex("class=\"\"((.|\n)*?)align=\"absbottom\"",
                RegexOptions.IgnoreCase);
           
            for(Match itemMatch = keyRegex.Match(oM.Value);itemMatch.Success;itemMatch = itemMatch.NextMatch())
            {
                Regex productName = new Regex("alt=\"((.|\n)*?)\"",
                       RegexOptions.IgnoreCase);
 
                Regex productPrice = new Regex("class=\"saleprice\">((.|\n)*?)</span>",
                       RegexOptions.IgnoreCase);
                Match productValue = productPrice.Match(oM.Value);
 
 
                
                if (itemMatch.Value.ToLower().IndexOf(keywords.ToLower()) != -1)
                {
                    Match finalName = productName.Match(itemMatch.Value.ToLower());
 
                    
                    dataTable.Rows.Add(finalName.Value, productValue.Value);
                    
                }
                
            }
 
            
            foreach(DataRow row in dataTable.Rows)
            {
             //I want to add "name" of the row to the productNameArray
             //and the price to productPriceArray
                productNameArray.Add(row(0));
 
            }

Open in new window

Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Just get each value from the row and add to the lists. You use square brackets to access the items by index:
foreach(DataRow row in dataTable.Rows) {
   productNameArray.Add((string)row[0]);
   productPriceArray.Add((string)row[1]);
}

Open in new window

Avatar of nsanga
nsanga

Just use this

foreach(DataRow row in dataTable.Rows)
            {
             //I want to add "name" of the row to the productNameArray
             //and the price to productPriceArray
                productNameArray.Add(row.Item("Name").tostring());
                productPriceArray.Add(row.Item("Price").tostring());  
               
 
            }
Note:

The indexer for the DataRow returns the type Object, so you have to cast the values to string when adding them to a List<string>.

Reflection:

I don't really see the point of the DataSet and DataTable here, you could add the values directly to the lists.
Avatar of asif07

ASKER

How do i return the string and display it on the website.

When i do productNameArray.toString, it is displaying:System.Collections.Generic.List`1[System.String]  

I want to display the contents of the TWO arrays name and price on the website.
You could use a repeater on the page, but then you would rather use the DataTable as data source, as you can only assign one data source, not two.

You can loop the lists and add elements to the page for each item. You add elements to the Controls collection of a control, for example a PlaceHolder:

for (int i=0; i<productNameArray.Count; i++) {
   HtmlGenericControl label = new HtmlGenericControl("label");
   label.Attributes.Add("class", "ProductLabel");
   label.InnerText = productNameArray[i];
   PlaceHolder1.Controls.Add(label);
   HtmlGenericControl span = new HtmlGenericControl("span");
   span.Attributes.Add("class", "ProductPrice");
   span.InnerText = productPriceArray[i];
   PlaceHolder1.Controls.Add(span);
}
Its not much clear of what you are trying to do. You will get better solution only if you give your actual requirement.
Avatar of asif07

ASKER

In the code, I have scraped amazon.com for searching any item which it takes in the parameter as keywords.  I set a label in the default.aspx and it returns the price and the item name!  Then, I looped over the page for each match.---for(Match itemMatch =keyRegex.Match(oM.Value);itemMatch.Success;itemMatch = itemMatch.NextMatch()).  Now, I want to display the name and the respective price in my web page.  I took datasets to fill tables with rows for each item and two columns titled name and price.

How do I display this table in my web page?
For example, if kodak return 16 results, my dataset has 16 rows how do I display those 16 rows in my web page.  What do I return?
Read my last reply above. It seams like you missed it...
ASKER CERTIFIED SOLUTION
Avatar of nsanga
nsanga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you initially know the tables count, then you can drag and drop that many datagrid controls to your page. If not just use my code to dynamically add the datagrids based on your result.
> I that case you need a datagrid or gridview in your default.aspx page

That is simply not true. There are plenty of other options to display data like this.
i don't mean datagrid or gridview is the only way to display data in a datatable. But the easiest and structured way to display data.