ARTICLE

Implementing Search in a GridView using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 05, 2011
Here, we will see how to implement Search in a GridView in ASP.NET.
 
Reader Level:

To display data from my database and after the user enter name to search in TextBox and hits the search button. GridView displays the results on the same page. This is the important functionality of web page. To do that we create a table in SQL Server database.

Creating Table

Now creates a table with the ID, name, city, country. Table has named as location. Table looks like below.

search1.gif

Figure1

Now drag and drop 1 TextBox, 1 Button and 1 GridView control on the form.

search2.gif

Figure2

.aspx code

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

    <div>

     <asp:Label id="lblKeyword" runat="server" text="Keyword"></asp:Label>

        <asp:TextBox id="txtKeyWord" runat="server"></asp:TextBox>

        <asp:Button id="btnSearch" onclick="btnSearch_Click" runat="server" Text="Search"></asp:Button>

        <br />

        <br />

        <asp:GridView id="myGridView" runat="server" AllowPaging="True" AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound" OnPageIndexChanging="ShowPageCommand" PageSize="2">

            <HeaderStyle backcolor="#cccccc"></HeaderStyle>

            <AlternatingRowStyle backcolor="#e8e8e8"></AlternatingRowStyle>

            <Columns>

                <asp:TemplateField HeaderText="ID">

                    <ItemTemplate>

                        <asp:Label id="lblID" runat="server"></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Name">

                    <ItemTemplate>

                        <asp:Label id="lblName" runat="server"></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="City">

                    <ItemTemplate>

                        <asp:Label id="lblCity" runat="server"></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Country">

                    <ItemTemplate>

                        <asp:Label id="lblCountry" runat="server"></asp:Label>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

    </form>

.cs code

Imports System.Data

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

    Dim strKeyWord As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        strKeyWord = Me.txtKeyWord.Text

    End Sub

    Sub BindData()

 

        Dim objConn As New SqlConnection

        Dim objCmd As New SqlCommand

        Dim dtAdapter As New SqlDataAdapter

        Dim ds As New DataSet

        Dim strConnString, strSQL As String

 

        strConnString = "Server=.;UID=sa;PASSWORD=Password$2;database=Details;"

        strSQL = "SELECT * FROM Location WHERE (Name like '%" & strKeyWord & "%') "

 

        objConn.ConnectionString = strConnString

        With objCmd

            .Connection = objConn

            .CommandText = strSQL

            .CommandType = CommandType.Text

        End With

        dtAdapter.SelectCommand = objCmd

 

        dtAdapter.Fill(ds)

 

        '*** BindData to GridView ***'

        myGridView.DataSource = ds

        myGridView.DataBind()

 

        dtAdapter = Nothing

        objConn.Close()

        objConn = Nothing

 

    End Sub

 

    Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

 

        Dim lblCustomerID As Label = CType(e.Row.FindControl("lblID"), Label)

        If Not IsNothing(lblCustomerID) Then

            lblCustomerID.Text = e.Row.DataItem("ID")

        End If

 

 

        Dim lblName As Label = CType(e.Row.FindControl("lblName"), Label)

        If Not IsNothing(lblName) Then

            lblName.Text = e.Row.DataItem("Name")

        End If

 

 

        Dim lblEmail As Label = CType(e.Row.FindControl("lblCity"), Label)

        If Not IsNothing(lblEmail) Then

            lblEmail.Text = e.Row.DataItem("City")

        End If

 

 

        Dim lblCountryCode As Label = CType(e.Row.FindControl("lblCountry"), Label)

        If Not IsNothing(lblCountryCode) Then

            lblCountryCode.Text = e.Row.DataItem("Country")

        End If

 

    End Sub

 

    Sub ShowPageCommand(ByVal s As Object, ByVal e As GridViewPageEventArgs)

        myGridView.PageIndex = e.NewPageIndex

        BindData()

    End Sub

 

    Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)

        BindData()

    End Sub

End Class

Now run the application and test it.

search3.gif

Figure3

Now enter the name to search from database.

search4.gif

Figure4

Now click on the search Button to display the search result.

search5.gif

Figure5

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor