Google Book Search in ASP.Net

Introduction

This article describes how to add a Google Book search feature in ASP.Net.  Here I will describe how to communicate with the Google Search API.

Description

Since it is third party software, we require a third-party DLL to be referenced by our application that will communicate with the Google server.

For this we must download the Google Search API:

  1. GoogleSearchAPI.dll

You can download it from the source code attached to this article.

Or from this link: http://google-api-for-dotnet.googlecode.com/files/GoogleSearchAPI_0.3.1.zip

Design

Now add one TextBox, one Button and one Datalist.

In the Datalist I used two HyperLinks to show the search result title with Book Image and Published Year.

One label to show Author Name and Number of Pages.

Design your screen as in the following screen.

Google-book-search-in-aspdotnet-1.jpg
Or you can copy the following source code:

 

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.         <asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>  
  5.         <asp:Button ID="btnSearch" runat="server" Text="Google Book Search" OnClick="Button1_Click" /><br />  
  6.         <asp:DataList ID="dlSearch" runat="server" RepeatColumns="6" CellSpacing="5">  
  7.             <ItemTemplate>  
  8.                 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url") %>'><img src='<%#Eval("Image") %>' width="120px" height="170px"/></asp:HyperLink><br />  
  9.                 <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#Eval("Url") %>'><%#Eval("Title")+"("+Eval("Year")+")" %></asp:HyperLink>  
  10.                 <asp:Label ID="Label1" runat="server" Text='<%#Eval("Author")+"(Pages:"+Eval("Page")+")" %>'></asp:Label>  
  11.             </ItemTemplate>  
  12.             <ItemStyle Width="250px" />  
  13.         </asp:DataList>  
  14.     </div>  
  15.     </form>  
  16. </body>

Now go to the code view.

Next add a reference of the following Google Search API DLL to your website:

  1. GoogleSearchAPI.dll

And write the following code in the .cs file:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Google.API.Search;  
  4. using System.Data;  
  5. public partial class BookSearch : System.Web.UI.Page  
  6. {  
  7.     protected void Page_Load(object sender, EventArgs e)  
  8.     {  
  9.         if (!IsPostBack)  
  10.         {  
  11.             dlSearch.DataSource = null;  
  12.             dlSearch.DataBind();  
  13.             TextBox1.Text = "";  
  14.         }  
  15.     }  
  16.     protected void Button1_Click(object sender, EventArgs e)  
  17.     {  
  18.         DataSet ds = new DataSet();  
  19.         DataTable dt = new DataTable();  
  20.         dt.Columns.Add(new DataColumn("Title"typeof(string)));  
  21.         dt.Columns.Add(new DataColumn("Author"typeof(string)));  
  22.         dt.Columns.Add(new DataColumn("Url"typeof(string)));  
  23.         dt.Columns.Add(new DataColumn("Image"typeof(string)));  
  24.         dt.Columns.Add(new DataColumn("Year"typeof(string)));  
  25.         dt.Columns.Add(new DataColumn("Page"typeof(string)));  
  26.         GbookSearchClient client = new GbookSearchClient("www.c-sharpcorner.com");  
  27.         IList<IBookResult> results = client.Search(TextBox1.Text, 30);  
  28.         foreach (IBookResult result in results)  
  29.         {  
  30.             DataRow dr = dt.NewRow();  
  31.             dr["Title"] = result.Title.ToString();  
  32.             dr["Author"] = result.Authors.ToString();  
  33.             dr["Image"] = result.TbImage;  
  34.             dr["Url"] = result.Url.ToString();  
  35.             dr["Year"] = result.PublishedYear.ToString();  
  36.             dr["Page"] = result.PageCount.ToString();  
  37.             dt.Rows.Add(dr);  
  38.         }  
  39.         dlSearch.DataSource = dt;  
  40.         dlSearch.DataBind();  
  41.     }  
  42. }   

In the code above I passed the TextBox value in the button click to Google server.

After getting the result I bound it to the datatable then to the datalist control.

Just check these two lines:

  1. GbookSearchClient client = new GbookSearchClient ("www.c-sharpcorner.com");  
  2. IList<IBookResult> results = client.Search(TextBox1.Text, 30);  

In the first line I am passing "www.c-sharpcorner.com" as the Client because it required a hosted site for security purposes.

If you don't pass that then it will show an exception.

In the second line I am passing 30 and a TextBox value. Here 30 in other words I am getting 30 search results.

So you can increase or decrease this value as needed.

Now build your application. Enter a search query then press the button.

It will show all the Google Book search results.

Google-book-search-in-aspdotnet-2.jpg
Click on the Book to see the Details.


Similar Articles