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.
Figure1
Now drag and drop 1 TextBox, 1 Button and 1 GridView control on the form.
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) HandlesMe.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.
Figure3
Now enter the name to search from database.
Figure4
Now click on the search Button to display the search result.
Figure5