Subscribe For Free Updates!

We'll not spam mate! We promise.

Thursday 7 November 2013

Google Search In ASP NET

Introduction
 
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.

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

1.png
Or you can copy the following source code:


<
body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Google Search" OnClick="Button1_Click" /><br />
        <asp:DataList ID="dlSearch" runat="server" Width="600px">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Title") %>' PostBackUrl='<%#Eval("Url") %>'></asp:LinkButton><br />
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("Content") %>'></asp:Label><br />
                <br />
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label Visible='<%#bool.Parse((dlSearch.Items.Count==0).ToString())%>' runat="server"
                  ID="lblNoRecord" Text="No Record Found!"></asp:Label>
           </FooterTemplate>
        </asp:DataList>
    </div>
    </form>

</
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:
using System;
using
System.Collections.Generic;
using
Google.API.Search;
using
System.Data;
public
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dlSearch.DataSource = null;
            dlSearch.DataBind();
            TextBox1.Text = "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Title", typeof(string)));
        dt.Columns.Add(new DataColumn("Content", typeof(string)));
        dt.Columns.Add(new DataColumn("Url", typeof(string)));
        GwebSearchClient client = new GwebSearchClient("www.c-sharpcorner.com");
        IList<IWebResult> results = client.Search(TextBox1.Text, 30);
        foreach (IWebResult result in results)
        {
            DataRow dr = dt.NewRow();
            dr["Title"] = result.Title.ToString();
            dr["Content"] = result.Content.ToString();
            dr["Url"] = result.Url;
            dt.Rows.Add(dr);
        }
        dlSearch.DataSource = dt;
        dlSearch.DataBind();
    }
}
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:
GwebSearchClient client = new GwebSearchClient("www.c-sharpcorner.com");
IList<IWebResult> 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 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.

2.jpg
Now click on the link to see the corresponding site.
For any modifications or problems please comment. 
To download Source Click here
Thank You.

Monday 4 November 2013

GridView Sorting Using jQuery Plugin

Introduction

This article describes how to use GridView in ASP.Net and jQuery. We can do it server-side but if you do it using jQuery then you are putting less load on the server. You can handle it client-side.

Description

To create this application you need the jQuery plugins listed below.

  • jquery.tablesorter.min.js
  • jquery-1.4.3.min.js
You can download them from the source code attached in this page.

Design

Add a GridView with AutogeneratedColumn=false and apply any design.

Now design your screen like the following screen:

1.jpeg

Or you can copy the following source code:


<form id="form1" runat="server">
     <div>
         <asp:GridView ID="gvDept" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
             <AlternatingRowStyle BackColor="PaleGoldenrod" />
             <Columns>
                 <asp:BoundField DataField="DEPTNO" HeaderText="Id">
                     <ItemStyle Width="50px" />
                 </asp:BoundField>
                 <asp:BoundField DataField="DNAME" HeaderText="Dept Name" />
                 <asp:BoundField DataField="LOC" HeaderText="Location" />
             </Columns>
             <FooterStyle BackColor="Tan" /> 
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
  HorizontalAlign="Center" />
             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
             <SortedAscendingCellStyle BackColor="#FAFAE7" /> 
            <SortedAscendingHeaderStyle BackColor="#DAC09E" /> 
            <SortedDescendingCellStyle BackColor="#E1DB9C" />
             <SortedDescendingHeaderStyle BackColor="#C2A47B" />
         </asp:GridView>
     </div>
     </form>
 Next add the following JavaScript and CSS style code in the head tag (it's used for sorting):<style type="text/css">        th
        {
            cursor: pointer;
            background-color: #dadada;
            color: Black;
            font-weight: bold;
            text-align: left;
        }
        th.headerSortUp
        {
            background-image: url(images/asc.gif);
            background-position: right center;
            background-repeat: no-repeat;
        }
        th.headerSortDown
        {
            background-image: url(images/desc.gif);
            background-position: right center;
            background-repeat: no-repeat;
        }
        td
        {
            border-bottom: solid 1px #dadada;
        }
    </style>    

<script src="scripts/jquery-1.4.3.min.js" type="text/javascript"></script>    
<script src="scripts/jquery.tablesorter.min.js" type="text/javascript"></script>     
<script type="text/javascript">       
 $(document).ready(function () {
            $("#gvDept").tablesorter();
        });
    </script>


Now go to the code view.

Write the BindGrid method using the database or you can use a static datatable.

After binding, write these two lines that are requeired for sorting:
gvDept.UseAccessibleHeader = true;
gvDept.HeaderRow.TableSection = TableRowSection.TableHeader;
And write the following code .cs file: 

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Web; 
using System.Web.UI;
 using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page{
    private void BindGrid()
    {
        SqlConnection con = new SqlConnection("Data Source=Sanjeeb;database=MYDB;user id=test;password=Test");
        SqlCommand cmd = new SqlCommand("select * from DEPT", con);
        SqlDataAdapter dr = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dr.Fill(ds);
        gvDept.DataSource = ds;
        gvDept.DataBind();
        gvDept.UseAccessibleHeader = true;
        gvDept.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
    }
}


Now build your application. Click on every header; it will sort the corresponding grid column with ascending and descending image.

2.jpeg

For any modifications or problems please comment.
For download source Click Here

Thanks.