Hi sthsyed,
Check this example. Now please take its reference and correct your code.
HTML
Search MobileNo:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="Button1" Text="Search" runat="server" OnClick="Search" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" />
<asp:BoundField DataField="MobileNo" HeaderText="MobileNo" />
<asp:BoundField DataField="HomeNo" HeaderText="HomeNo" />
<asp:BoundField DataField="HomeMobileNo" HeaderText="HomeMobileNo" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Search();
}
}
private void Search()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
string sql = "SELECT PhoneNo, MobileNo, HomeNo, HomeMobileNo FROM SearchMobileNo";
cmd.CommandText = sql;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
protected void Search(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
string sql = "SELECT PhoneNo, MobileNo, HomeNo, HomeMobileNo FROM SearchMobileNo WHERE SUBSTRING(MobileNo,0,9) LIKE @MobileNo + '%' OR SUBSTRING(PhoneNo,1,8) LIKE @MobileNo + '%' OR SUBSTRING(HomeNo,1,8) LIKE @MobileNo + '%' OR SUBSTRING(HomeMobileNo,1,8) LIKE @MobileNo + '%' ";
if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
{
cmd.Parameters.AddWithValue("@MobileNo", txtSearch.Text.Trim());
}
cmd.CommandText = sql;
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.Search()
End If
End Sub
Private Sub Search()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
Dim sql As String = "SELECT PhoneNo, MobileNo, HomeNo, HomeMobileNo FROM SearchMobileNo"
cmd.CommandText = sql
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
Dim sql As String = "SELECT PhoneNo, MobileNo, HomeNo, HomeMobileNo FROM SearchMobileNo WHERE SUBSTRING(MobileNo,0,9) LIKE @MobileNo + '%' OR SUBSTRING(PhoneNo,1,8) LIKE @MobileNo + '%' OR SUBSTRING(HomeNo,1,8) LIKE @MobileNo + '%' OR SUBSTRING(HomeMobileNo,1,8) LIKE @MobileNo + '%' "
If Not String.IsNullOrEmpty(txtSearch.Text.Trim()) Then
cmd.Parameters.AddWithValue("@MobileNo", txtSearch.Text.Trim())
End If
cmd.CommandText = sql
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot