Solved

ASP.net C# Bind a Yahoo RSS Feed to a Gridview

Posted on 2008-10-18
3
2,154 Views
Last Modified: 2013-12-17
Hi,
I am trying to get code to read from RSS Feed to a GridView in ASP.Net C#
I have the c# code in App_Code folder CodeCS and VB code in App_Code folder CodeVB
and referenced the folders in Web.config as follows:
<codeSubDirectories><add directoryName="VBCode"/><add directoryName="CSCode"/>
I am getting the following errors:

Error      20      An object reference is required for the non-static field, method, or property
'System.Web.UI.WebControls.BaseDataBoundControl.DataSource.get'      
C:\Projects\Web\AssetAllocation\App_Code\CodeCS\FeedRss.aspx.cs
Line17       Column 9      GridView.DataSource = GetDataSet().Tables[3];

Error      21      An object reference is required for the non-static field, method, or property
'System.Web.UI.Control.DataBind()'      C:\Projects\Web\AssetAllocation\App_Code\CodeCS\FeedRss.aspx.cs
Line18       Column 9      GridView.DataBind();

Error      22      An object reference is required for the non-static field, method, or property
'System.Web.UI.WebControls.DataBoundControl.DataMember.get'      
C:\Projects\Web\AssetAllocation\App_Code\CodeCS\FeedRss.aspx.cs      25
Line 25      Column 9      GridView.DataMember = "item";

Error      23      An object reference is required for the non-static field, method, or property
'System.Web.UI.WebControls.BaseDataBoundControl.DataSource.get'      
C:\Projects\Web\AssetAllocation\App_Code\CodeCS\FeedRss.aspx.cs
Line 26      Column 9      GridView.DataSource = ds;

Error      21      'System.Web.UI.Page' does not contain a definition for 'DataItem'
and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Page'
could be found (are you missing a using directive or an assembly reference?)      
c:\Projects\Web\AssetAllocation\FeedRssCS.aspx
Line 26      Column 37      DataItem
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class rssCS : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView.DataSource = GetDataSet().Tables[3];
        GridView.DataBind();
    }
    private DataSet GetDataSet()
        
    {
        DataSet ds = new DataSet("rsRSS");
        ds.ReadXml("http://rss.news.yahoo.com/rss/mostviewed");
        GridView.DataMember = "item";
        GridView.DataSource = ds;
        //GridView1.DataBind();
        return ds;
    }
 
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        DateTime CurrTime = DateTime.Now.Date;
 
        if (row.RowIndex >= 0) // Start looping on the first row
            if (row.RowType == DataControlRowType.DataRow)
            {
                row.Cells[0].Text = row.Cells[0].Text;
                row.Cells[1].Text = row.Cells[1].Text;
            }
 
        }
 
 
}
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/App_Code/CodeCS/FeedRss.aspx.cs" Inherits="rssCS" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<!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>FeedRss</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView
        id="GridView"
        DataSourceId="srcMovies"
        Runat="server" />
        <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" NavigateUrl=<%#DataBinder.Eval(Container.DataItem,"link") %> 
        runat="server">
        <%#DataBinder.Eval(Container.DataItem,"title") %>
        </asp:HyperLink>
        </ItemTemplate>
        </asp:GridView>
<!--
     See TextFile.txt
-->
    <asp:SqlDataSource
        id="srcMovies"
        ConnectionString="Data Source=.\SQLExpress;
            AttachDbFilename=|DataDirectory|MyDatabase.mdf;
            Integrated Security=True;User Instance=True"
        SelectCommand="SELECT * FROM Movies"
        Runat="server" />
    </div>
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:homeshopper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22755083
why are you trying to give 2 datasources to the grid view
one datasource is when you load the grid view and is using u r local database
the second one is the dataset which you prepare from the rss feed

just try to provide only one datasource for your gridview and i think it should work fine

one more thing i noticed
GridView.DataMember = "item"; in the function GetDataSet()
you even dont need this line if you are specifying your columns in the grid view definition
0
 

Author Comment

by:homeshopper
ID: 22761271
Hi ragi0017,
Sorry not to have got back to your earlier.
I tried following your suggestions, not sure if I have got it right yet.
I made some alterations and the new code is attached.
Still getting 1 error now:
Error      38      'System.Web.UI.Page' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'System.Web.UI.Page' could be found (are you missing a using directive or an assembly reference?)      c:\Projects\Web\AssetAllocation\FeedRssCS.aspx      26      38      http://localhost/AssetAllocation/

<%#DataBinder.Eval(Container.DataItem,"title") %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class rssCS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /// delete this line----GridView.DataSource = GetDataSet().Tables[3];
        ///delete this line----GridView.DataBind();
    }
    private DataSet GetDataSet()    
    {
        DataSet ds = new DataSet("rsRSS");
        ds.ReadXml("http://rss.news.yahoo.com/rss/mostviewed");
        ///delete this line----GridView.DataMember = "item";
        ///delete this line----GridView.DataSource = ds;
        //delete this line----GridView1.DataBind();
        return ds;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        DateTime CurrTime = DateTime.Now.Date;
        if (row.RowIndex >= 0) // Start looping on the first row
            if (row.RowType == DataControlRowType.DataRow)
            {
                row.Cells[0].Text = row.Cells[0].Text;
                row.Cells[1].Text = row.Cells[1].Text;
            }
        }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/App_Code/CodeCS/FeedRss.aspx.cs" Inherits="rssCS" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<!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>FeedRss</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView
        id="GridView"
        DataSourceId="srcMovies"
        Runat="server" />
        <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" NavigateUrl=<%#DataBinder.Eval(Container.DataItem,"link") %> 
        runat="server">
        <%#DataBinder.Eval(Container.DataItem,"title") %>
        </asp:HyperLink>
        </ItemTemplate>
        </asp:GridView>
<!--
-->
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question