Hi @Gani_tpt,
Here, i am getting the error while i'm entering the name or salary or doj..
Do you mean that only these fields will go wrong?
I tested your code and found several issues.
If you check your html, you will find that there is a line missing <asp:TemplateField ItemStyle-HorizontalAlign="Center">
.
And the ajax url
part, use the name you created when you created the aspx.
Another thing to note is that because the string type cannot be converted to the int type, you need to make a judgment.
The following is an example of my test for your reference.
CREATE TABLE [dbo].[View_Cust_Details]
(
[Id] INT NOT NULL PRIMARY KEY,
[CustNo] INT NULL,
[CustName] NVARCHAR(50) NULL,
[Department] NVARCHAR(50) NULL,
[MaterialName] NVARCHAR(50) NULL,
[Salary] INT NULL,
[DateofJoining] NVARCHAR(50) NULL
)
CREATE PROCEDURE Sp_Get_Cust_Details
@Sp_SearchText NVARCHAR(50)
AS
if ISNUMERIC(@Sp_SearchText)=1
Begin
SELECT Id,CustNo FROM View_Cust_Details
WHERE CustNo LIKE @Sp_SearchText + '%'
UNION ALL
SELECT Id,Salary FROM View_Cust_Details
WHERE Salary LIKE @Sp_SearchText + '%'
End
Else
Begin
SELECT Id,CustName FROM View_Cust_Details
WHERE CustName LIKE @Sp_SearchText + '%'
UNION ALL
SELECT Id,Department FROM View_Cust_Details
WHERE Department LIKE @Sp_SearchText + '%'
UNION ALL
SELECT Id,MaterialName FROM View_Cust_Details
WHERE MaterialName LIKE @Sp_SearchText + '%'
UNION ALL
SELECT Id,DateofJoining FROM View_Cust_Details
WHERE DateofJoining LIKE @Sp_SearchText + '%'
End
GO
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css'
media="screen" />
<script type="text/javascript" src='https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js'></script>
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js'></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bassjobsen/Bootstrap-3-Typeahead/master/bootstrap3-typeahead.min.js"></script>
<link rel="Stylesheet" href="https://twitter.github.io/typeahead.js/css/examples.css" />
<style type="text/css">
.typeahead.dropdown-menu {
height: 300px;
overflow-y: auto;
}
</style>
<script type="text/javascript">
$(function () {
$('[id*=txtSearch]').typeahead({
hint: true,
highlight: true,
minLength: 1,
items: 'all'
, source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/WebForm1.aspx/GetSections") %>',
data: "{ 'prefix': '" + request + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
items = [];
map = {};
$.each(data.d, function (i, item) {
var id = item.split('-')[0];
var name = item.split('-')[1];
map[name] = { id: id, name: name };
items.push(name);
});
response(items);
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
updater: function (item) {
$('[id*=hfSectionId]').val(map[item].id);
return item;
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter search term:
<asp:TextBox ID="txtSearch" runat="server" CssClass="form-control" autocomplete="off"
Width="300" />
<br />
<asp:HiddenField ID="hfSectionId" runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
</div>
<div>
<asp:GridView ID="gvcustomer" AutoGenerateColumns="False" runat="server"
Width="98%" DataKeyNames="Id">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
CustNo</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCustNo" runat="server" Text='<%# Eval("CustNo")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
CustName</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCustName" runat="server" Text='<%# Eval("CustName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
Department</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server" Text='<%# Eval("Department")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
MaterialName</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblMaterialName" runat="server" Text='<%# Eval("MaterialName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
Salary</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSalary" runat="server" Text='<%# Eval("Salary")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<label style="text-align: center; display: block;">
DateOfJoining</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDateOfJoining" runat="server" Text='<%# Eval("DateOfJoining")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * from View_Cust_Details"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
ViewState["dirState"] = dt;
gvcustomer.DataSource = dt;
gvcustomer.DataBind();
}
}
}
}
}
[System.Web.Services.WebMethod]
public static string[] GetSections(string prefix)
{
List<string> sections = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Sp_Get_Cust_Details";
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@Sp_TYPE", "SearchTxT_CustDetails");
cmd.Parameters.AddWithValue("@Sp_SearchText", prefix);
cmd.Connection = conn;
conn.Open();
int i;
bool bNum = int.TryParse(prefix, out i);
if (bNum == true)
{
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sections.Add(string.Format("{0}-{1}", sdr["Id"], sdr["CustNo"]));
}
}
}
else
{
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sections.Add(string.Format("{0}-{1}", sdr["Id"], sdr["CustName"]));
}
}
}
conn.Close();
}
}
return sections.ToArray();
}
protected void Submit(object sender, EventArgs e)
{
string Bill_Id = Request.Form[txtSearch.UniqueID];
string EmpNo = Request.Form[hfSectionId.UniqueID];
//call grid and filter the grid view based on text enter search
if (ViewState["dirState"] != null)
{
DataTable dt = ViewState["dirState"] as DataTable;
//make a clone of the datatable
DataTable dtNew = dt.Clone();
//search the datatable for the correct fields
foreach (DataRow row in dt.Rows)
{
//add your own columns to be searched here
if (row["CustNo"].ToString().Contains(Bill_Id) || row["CustName"].ToString().Contains(Bill_Id) || row["Department"].ToString().Contains(Bill_Id) || row["MaterialName"].ToString().Contains(Bill_Id) || row["Salary"].ToString().Contains(Bill_Id) || row["DateofJoining"].ToString().Contains(Bill_Id))
{
//when found copy the row to the cloned table
dtNew.Rows.Add(row.ItemArray);
}
}
//rebind the grid
gvcustomer.DataSource = dtNew;
gvcustomer.DataBind();
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.