How to Search News in Google Using ASP.Net

Introduction

This article explains how to search the news in Google using ASP.NET.

In this article we will use the GoogleSearchAPI to find today's news based on the keywords used.

Step 1

First start a new project in ASP.NET and name it as "GoogleNews".

news1.jpg

Step 2

Now add a new web page to your application and again name it "GoogleNews".

news2.jpg

Now you need to add a DLL for Google News, you can find the DLL in the file attached to this article.

news3.jpg

Step 3

Now you need to add the following code to your application:

<body>

    <form id="form1" runat="server">

    <div>

        <asp:TextBox ID="SearchBox" runat="server" Width="300px"></asp:TextBox>

        <asp:Button ID="btnSearch" runat="server"

        Text="See Today's News" OnClick="Button1_Click"/><br />

        <asp:DataList ID="dtlist" runat="server" Width="600px">

            <ItemTemplate>

                <asp:LinkButton ID="lnk" runat="server" Text='<%#Eval("Heading") %>' PostBackUrl='<%#Eval("Path") %>'></asp:LinkButton><br />

                <asp:Label ID="Lbl1" runat="server" Text='<%#Eval("Data") %>'></asp:Label><br/>

                <img src='<%#Eval("Pic") %>'  width="100px" height="80px" /><br />

                <asp:Label ID="Lbl2" runat="server" Text='<%# "(Publisher:"+Eval("Publisher")+")"+ Eval("Date") %>'></asp:Label>

                <br />

            </ItemTemplate>

            <FooterTemplate>

                <asp:Label Visible='<%#bool.Parse((dtlist.Items.Count==0).ToString())%>' runat="server" ID="NoNews" Text="Sry, No News"></asp:Label>

            </FooterTemplate>

        </asp:DataList>

    </div>

    </form>

</body>

This will add a TextBox, a Button and a DataList to your application.

Three more things are used in this application, these are:

  1. A Link Label to show the title of the search results.
  2. An Image Control to show the image of the news.
  3. And a Label to show the Publisher Name and the date of the news.

Your application will now have a design like this:

news4.jpg

Step 4

Add these two namespaces to your project:

using Google.API.Search;

using System.Data;

After adding these namespaces, add the following code to your application:

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                dtlist.DataSource = null;

                dtlist.DataBind();

                SearchBox.Text = "";

            }

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            DataSet dts = new DataSet();

            DataTable data = new DataTable();

            data.Columns.Add(new DataColumn("Heading", typeof(string)));

            data.Columns.Add(new DataColumn("Data", typeof(string)));

            data.Columns.Add(new DataColumn("Path", typeof(string)));

            data.Columns.Add(new DataColumn("Pic", typeof(string)));

            data.Columns.Add(new DataColumn("Publisher", typeof(string)));

            data.Columns.Add(new DataColumn("Date", typeof(string)));

            GnewsSearchClient client = new GnewsSearchClient("");

            IList<INewsResult> results = client.Search(SearchBox.Text, 30);

            foreach (INewsResult result in results)

            {

                DataRow dtr = data.NewRow();

                dtr["Heading"] = result.Title.ToString();

                dtr["Data"] = result.Content.ToString();

                dtr["Path"] = result.Url;

                dtr["Pic"] = result.Image;

                dtr["Publisher"] = result.Publisher;

                dtr["Date"] = result.PublishedDate;

                data.Rows.Add(dtr);

            }

            dtlist.DataSource = data;

            dtlist.DataBind();

        }

This code will send the value of the text box to the Google Server, on getting the results it will be bound to the DataTable and then to the DataList.

Output

It's final output will be as follows:

news5.jpg

First a blank TextBox will be available in which you need to enter the Keyword by which you want to search the news.

After providing the keyword, click on the Button in front of it.

news6.jpg


Recommended Free Ebook
Similar Articles