Pleaes refer this code
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Text
<asp:TextBox ID="txtSearch" runat="server" />
<br />
Search based on
<br />
<asp:RadioButton ID="rbtnContains" Text="Contains" GroupName="SearchType" runat="server" />
<br />
<asp:RadioButton ID="rbtnStartWith" Text="Start with" GroupName="SearchType" runat="server" />
<br />
<asp:RadioButton ID="rbtnEndsWith" Text="Ends with" GroupName="SearchType" runat="server" />
<br />
<asp:Button Text="Filter Customer" OnClick="Filter" runat="server" />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
C#
private void PopulateCustomers()
{
if (!this.rbtnContains.Checked && !this.rbtnEndsWith.Checked && !this.rbtnStartWith.Checked)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert ('Please select any option')", true);
}
else
{
string sqlQuery = string.Empty;
if (rbtnContains.Checked)
{
sqlQuery = string.Format("SELECT * FROM Customers WHERE Name LIKE '%{0}%'", this.txtSearch.Text.Trim());
}
if (this.rbtnStartWith.Checked)
{
sqlQuery = string.Format("SELECT * FROM Customers WHERE Name LIKE '{0}%'", this.txtSearch.Text.Trim());
}
if (this.rbtnEndsWith.Checked)
{
sqlQuery = string.Format("SELECT * FROM Customers WHERE Name LIKE '%{0}'", this.txtSearch.Text.Trim());
}
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
}
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download database SQL from here.
Screenshot