Google Search In ASP NET


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


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 in this article.


Now add one TextBox,one Button and one Datalist.

In the Datalist I used one linkbutton to show the search result title with link and label to show the search description.

Design your screen as in the following screen.

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 Search" OnClick="Button1_Click" /><br />  
  6.         <asp:DataList ID="dlSearch" runat="server" Width="600px">  
  7.             <ItemTemplate>  
  8.                 <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Title") %>' PostBackUrl='<%#Eval("Url") %>'></asp:LinkButton><br />  
  9.                 <asp:Label ID="Label1" runat="server" Text='<%#Eval("Content") %>'></asp:Label><br />  
  10.                 <br />  
  11.             </ItemTemplate>  
  12.             <FooterTemplate>  
  13.                 <asp:Label Visible='<%#bool.Parse((dlSearch.Items.Count==0).ToString())%>' runat="server"  
  14.                   ID="lblNoRecord" Text="No Record Found!"></asp:Label>  
  15.            </FooterTemplate>  
  16.         </asp:DataList>  
  17.     </div>  
  18.     </form>  
  19. </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 _Default : 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("Content"typeof(string)));  
  22.         dt.Columns.Add(new DataColumn("Url"typeof(string)));  
  23.         GwebSearchClient client = new GwebSearchClient("");  
  24.         IList<IWebResult> results = client.Search(TextBox1.Text, 30);  
  25.         foreach (IWebResult result in results)  
  26.         {  
  27.             DataRow dr = dt.NewRow();  
  28.             dr["Title"] = result.Title.ToString();  
  29.             dr["Content"] = result.Content.ToString();  
  30.             dr["Url"] = result.Url;  
  31.             dt.Rows.Add(dr);  
  32.         }  
  33.         dlSearch.DataSource = dt;  
  34.         dlSearch.DataBind();  
  35.     }  
  36. }  

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. GwebSearchClient client = new GwebSearchClient("");  
  2. IList<IWebResult> results = client.Search(TextBox1.Text, 30);  

In the first line I am passing "" as the Client because it required a hosted site for Security purposes.

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

In the second line I am passing 30 and a TextBox value. Here 30 means 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 search results.


Now click on the link to see the corresponding site.

For any modifications or problems please comment.

Thank You.

Similar Articles