How to use caching with gridview for rss news feed

Here is a simple asp.net application to get rss news data and display on a gridview. It also uses caching.


Private Function GetRSSFeed(ByVal link As String, ByVal proxy As String, ByVal gridViewControl As GridView, ByVal cachename As String) As String

       'called from timer
        Dim myxml As XmlDataSource = New XmlDataSource
        myxml = CType(Cache.Item(cachename), XmlDataSource)

        If IsNothing(myxml) Then
            lbError.Text = "if cache is null then fetch"
            myxml = New XmlDataSource  'myxml is null so reset it
            myxml.DataFile = link
            myxml.XPath = "rss/channel/item"

            Try
                Cache.Insert(cachename, myxml, Nothing, Now.AddMinutes(2), TimeSpan.Zero)
                'nameof ur cache, datasource, null , no of minutes or hours to survive, timespan
                gridViewControl.DataSource = myxml
                gridViewControl.DataBind()
                '  lbError.Visible = False
                Return "1"
            Catch ex As Exception
                'gvLiveNews.Visible = False
                Return "Unable to get Portion of Live News Feed from internet. Service shall be restored shortly."
            End Try
        Else
            lbError.Text = "Getting from cache"
            gridViewControl.DataSource = myxml
            gridViewControl.DataBind()
            Return "1"
        End If
    End Function

call this function from a timer and updatepanel to get asynchronus postback.

Protected Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        '   loadLiveNews()
        GetRSSFeed("http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=ASI|IN|IN033|bhatwari", "191.254.186.111:5678", gvTemp, "gvTemp")
        GetRSSFeed("http://www.bseindia.com/sensex/xml-data/sensexrss.xml", "191.254.186.111:5678", gvBSE, "gvBSE")
        GetRSSFeed("http://rss.news.yahoo.com/rss/india", "191.254.186.111:5678", gvLiveNews, "gvLiveNews")
        GetRSSFeed("http://news.google.com/news?cf=all&ned=hi_in&hl=hi&output=rss", "191.254.253.60:8080", gvLiveNews2, "gvLiveNews2")
        Timer2.Enabled = False

    End Sub

aspx code

              <h3>Live News </h3>
    
        
    
    <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional"  >
    <ContentTemplate>
         
         <asp:GridView ID="gvLiveNews" runat="server" AutoGenerateColumns="False"  CellPadding="2" ForeColor="#333333"
            GridLines="None" Style="position: static"  EmptyDataText="No News to show" Font-Size="10px" Width="137px" BorderColor="#3399FF" 
                      BorderStyle="Dotted" BorderWidth="1px" AllowPaging="True"   PageSize="4" ShowHeader="False"  >
               <PagerSettings Visible="False" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
               <asp:TemplateField  ShowHeader="False">
               <ItemTemplate>
                <a href="loader.aspx?requestedpage=livenews" ><font color="black"  > <%# XPath("title")%> </font> </a>
               </ItemTemplate>
               <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>  
            </Columns>
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

         <asp:GridView ID="gvLiveNews2" runat="server" AutoGenerateColumns="False"  CellPadding="2" ForeColor="#333333"
            GridLines="None" Style="position: static"  EmptyDataText="No News to show" Font-Size="10px" Width="137px" BorderColor="#3399FF" 
                      BorderStyle="Dotted" BorderWidth="1px" AllowPaging="True"   PageSize="4" ShowHeader="False"   >
               <PagerSettings Visible="False" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
               <asp:TemplateField  ShowHeader="False">
               <ItemTemplate>
                <a href="loader.aspx?requestedpage=livenews" ><font color="black"  > <%#XPath("title")%> </font> </a>
               </ItemTemplate>
               <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>  
            </Columns>
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

        
           <asp:Label ID="lbError" runat="server" Text="Getting News Feed.. Please Wait..." > <asp:Image  ID="imgLoad2" runat="server" ImageUrl="images/loaderbar.gif"  />  </asp:Label>
        <asp:Timer ID="Timer2" runat="server" Interval="2000">
        </asp:Timer>
        </ContentTemplate>
       
        </asp:UpdatePanel>

Comments

Prashanth said…
This comment has been removed by the author.