Hi Bhavesh23,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
First: <asp:TextBox ID="txtFirst" runat="server" /><br />
Middle: <asp:TextBox ID="txtMiddle" runat="server" /><br />
Last: <asp:TextBox ID="txtLast" runat="server" /><br />
<asp:Button Text="Search" runat="server" OnClick="OnSearch" />
<hr />
<asp:Label ID="lblName" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void OnSearch(object sender, EventArgs e)
{
string first = txtFirst.Text.Trim();
string middle = txtMiddle.Text.Trim();
string last = txtLast.Text.Trim();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM " +
"( " +
" SELECT CustomerID, ContactName, City, Country, " +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 1)) AS FirstName, " +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 2)) AS MiddleName," +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 3)) AS LastName " +
" From Customers " +
")t " +
"WHERE t.FirstName LIKE '%' + @First + '%' " +
"AND t.MiddleName LIKE '%' + @Middle + '%' " +
"AND t.LastName LIKE '%' + @Last + '%'";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@First", first);
cmd.Parameters.AddWithValue("@Middle", middle);
cmd.Parameters.AddWithValue("@Last", last);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
this.lblName.Text = Convert.ToString(dt.Rows[0]["ContactName"]);
}
}
}
}
VB.Net
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Dim first As String = txtFirst.Text.Trim()
Dim middle As String = txtMiddle.Text.Trim()
Dim last As String = txtLast.Text.Trim()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM " +
"( " +
" SELECT CustomerID, ContactName, City, Country, " +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 1)) AS FirstName, " +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 2)) AS MiddleName," +
" REVERSE(PARSENAME(Replace(REVERSE(ContactName), ' ', '.'), 3)) AS LastName " +
" From Customers " +
")t " +
"WHERE t.FirstName LIKE '%' + @First + '%' " +
"AND t.MiddleName LIKE '%' + @Middle + '%' " +
"AND t.LastName LIKE '%' + @Last + '%'"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@First", first)
cmd.Parameters.AddWithValue("@Middle", middle)
cmd.Parameters.AddWithValue("@Last", last)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Me.lblName.Text = Convert.ToString(dt.Rows(0)("ContactName"))
End Using
End Using
End Using
End Sub