Senin, 02 April 2012

MEMBUAT RSSFEED DENGAN MENGGUNAKAN ASP.NET DENGAN VISUAL STUDIO 2008/2010 DAN SQLSERVER 2008R2


MEMBUAT RSSFEED DENGAN MENGGUNAKAN ASP.NET DENGAN VISUAL STUDIO 2008/2010 DAN SQLSERVER 2008R2

Kita persiapkan visual studio dan sqlserver 2008r2 atau 2008 kemudian setelah terintalasi di windows kita mulai dengan membuka visual studio kemuadian file new project  kemudian pilih …….. dan berinama filenya teserah kta dan akan beri saya nama rss kita simpan di tempat yang kita inginkan.




Setelah itu akan tampil seperti di bawah ini



kemudian kita buat file baru dengan cara klik kanan pada solution kemudian pilih pilih yang berextensi.aspx  seperti ngambar di bawah ini.



Dan untuk di web.config untuk connectionstring kita buat seperti ini yang mana sebelumnya kita sudah memiliki database dan membuat table di sqlserver 2008r2 fungsi dari code di bawah ini adalah sebagai penghubung ke data dari sqlserver ke visualstudio untuk code yang berwarna merah sebagai code penghubungnya.

<?xml version="1.0"?>

<configuration>


    <configSections>
      <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          </sectionGroup>
        </sectionGroup>
      </sectionGroup>
    </configSections> 

 
    <appSettings/>
  <connectionStrings>
    <add name="SiteSqlServer" connectionString="Data Source=ANDRIYANTO-PC;Initial Catalog=DB_WELASASIH;User ID=welasasih;Password=welasasih" providerName="System.Data.SqlClient" />
  </connectionStrings>
 
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="false">

          <assemblies>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          </assemblies>

        </compilation>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows" />
        <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->

      <pages>
        <controls>
          <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </controls>
      </pages>

      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
      </httpHandlers>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </httpModules>


    </system.web>

    <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                  type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="WarnAsError" value="false"/>
        </compiler>
      </compilers>
    </system.codedom>

    <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
    <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
      <modules>
        <remove name="ScriptModule" />
        <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </modules>
      <handlers>
        <remove name="WebServiceHandlerFactory-Integrated"/>
        <remove name="ScriptHandlerFactory" />
        <remove name="ScriptHandlerFactoryAppServices" />
        <remove name="ScriptResource" />
        <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </handlers>
    </system.webServer>

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
          <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
          <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
      </assemblyBinding>
    </runtime>

</configuration>

Setelah itu pada file yang sudah kita buat dengan visual studio td pada tahapan pertama dengan nama……. Kita isi dengan code seperti di bawah ini

<%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeBehind="FEED.aspx.cs"
    Inherits="RSSFEED.FEED" %>

<asp:repeater id="RepeaterRSS" runat="server"> 
        <HeaderTemplate> 
           <rss version="2.0"> 
            <channel>
          
            <title>TOKO BINTAROPLAZA</title>
             <pubDate>1 maret 2012</pubDate>   
            <link>http://ama.comlu.com</link> 
            <description>Bintaro plaza Pusat Swalayan Terbesar di Jakarta</description>   
            <language>id-ID</language>
            <webMaster>admin@metrotvnews.com</webMaster>
                          <image>
                          <title>BINTARO PLAZA</title>
                          <url>http://localhost/BintaroHeaderleft.jpg</url>
              <link>http://www.bintaropaza.com/</link>
                        </image> 
        </HeaderTemplate> 
        <ItemTemplate> 
            <item>
            <pubDate><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "pubdate"))%></pubDate>
            <title><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "title")) %></title> 
            <link><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "link"))%></link>      
            <description><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "description"))%></description>   
            <story><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "story"))%></story>   
            <image><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "image"))%></image>   
            <category><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "category"))%></category>                 
            <locationstore><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "locationstore"))%></locationstore> 
            <discount><%# RemoveIllegalCharacters(DataBinder.Eval(Container.DataItem, "discount"))%></discount>
            <guid><%# DataBinder.Eval(Container.DataItem, "guid") %></guid>  
            
        </item> 
        </ItemTemplate> 
        <FooterTemplate> 
                </channel> 
            </rss> 
        </FooterTemplate> 
</asp:repeater>         
            
            
             
              
              
                          
             
               
              
               
                <%--<pubDate><%# String.Format("{0:R}", DataBinder.Eval(Container.DataItem, "DatePublished"))%></pubDate>  --%>
             
               
              
      Dan untuk aspx.cs kita buat seperti di bawah ini untuk codenya

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace RSSFEED
{
    public partial class FEED : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // Get connection string from web.config file 
            string connString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;

            // Create SqlConnection object 
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = connString;

            // SQL query to retrieve data from database 
            string sqlQuery = "SELECT guid,discount,locationstore,category,image,story,description,link,title, pubdate FROM RSS ORDER BY pubdate DESC";

            // Create SqlCommand object 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlConn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlQuery;

            // open connection and then binding data into RepeaterRSS control 
            sqlConn.Open();
            RepeaterRSS.DataSource = cmd.ExecuteReader();
            RepeaterRSS.DataBind();
            sqlConn.Close();
        }
        protected string RemoveIllegalCharacters(object input)
        {
            // cast the input to a string 
            string data = input.ToString();

            // replace illegal characters in XML documents with their entity references 
            data = data.Replace("&", "&amp;");
            data = data.Replace("\"", "&quot;");
            data = data.Replace("'", "&apos;");
            data = data.Replace("<", "&lt;");
            data = data.Replace(">", "&gt;");

            return data;
        }
    }
}

Perlu diperhatikan sebelumnya kita sudah memiliki table di sqlserver dengan nama RSS dan memiliki beberapa atribut seperti guid, discount dan seperti isi code yang ada di aspx dan di bawahini adalah tampilajn  table rss yang ada di sqlserver:

Dan setelah itu kan akan run file aspx tersebut dengan kliknan –viewbrowser dan akan tampil seperti di bawah ini:

 Sekian ...

Tidak ada komentar:

Posting Komentar