Hi QuirkyKitt,
Check this example. Now please take its reference and correct your code.
For calling stored procedure using entity framework refer below article.
I have created the example using the below links.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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;
}
.Pager span
{
color: #333;
background-color: #F7F7F7;
font-weight: bold;
text-align: center;
display: inline-block;
width: 20px;
margin-right: 3px;
line-height: 150%;
border: 1px solid #ccc;
}
.Pager a
{
text-align: center;
display: inline-block;
width: 20px;
border: 1px solid #ccc;
color: #fff;
color: #333;
margin-right: 3px;
line-height: 150%;
text-decoration: none;
}
.highlight
{
background-color: #FFFFAF;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="ASPSnippets_Pager.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
GetCustomers(1);
});
$("[id*=txtSearch]").live("keyup", function () {
GetCustomers(parseInt(1));
});
$(".Pager .page").live("click", function () {
GetCustomers(parseInt($(this).attr('page')));
});
function SearchTerm() {
return jQuery.trim($("[id*=txtSearch]").val());
};
function GetCustomers(pageIndex) {
$.ajax({
type: "POST",
url: "Default.aspx/GetCustomers",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
var row;
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
if (row == null) {
row = $("[id*=gvCustomers] tr:last-child").clone(true);
}
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
if (customers.length > 0) {
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).html($(this).find("ContactName").text());
$("td", row).eq(1).html($(this).find("CustomerID").text());
$("td", row).eq(2).html($(this).find("City").text());
$("[id*=gvCustomers]").append(row);
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
var pager = xml.find("Pager");
$(".Pager").ASPSnippets_Pager({
ActiveCssClass: "current",
PagerCssClass: "pager",
PageIndex: parseInt(pager.find("PageIndex").text()),
PageSize: parseInt(pager.find("PageSize").text()),
RecordCount: parseInt(pager.find("RecordCount").text())
});
$(".ContactName").each(function () {
var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
$(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
});
} else {
var empty_row = row.clone(true);
$("td:first-child", empty_row).attr("colspan", $("td", row).length);
$("td:first-child", empty_row).attr("align", "center");
$("td:first-child", empty_row).html("No records found for the search criteria.");
$("td", empty_row).not($("td:first-child", empty_row)).remove();
$("[id*=gvCustomers]").append(empty_row);
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
Search:
<asp:TextBox ID="txtSearch" runat="server" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name"
ItemStyle-CssClass="ContactName" />
<asp:BoundField HeaderStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField HeaderStyle-Width="150px" DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
<br />
<div class="Pager">
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Reflection;
using System.Web.Services;
VB.Net
Imports System.Data
Imports System.Data.Objects
Imports System.Web.Services
Imports System.Reflection
Code
C#
private static int PageSize = 5;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDummyRow();
}
}
private void BindDummyRow()
{
DataTable dummy = new DataTable();
dummy.Columns.Add("CustomerID");
dummy.Columns.Add("ContactName");
dummy.Columns.Add("City");
dummy.Rows.Add();
gvCustomers.DataSource = dummy;
gvCustomers.DataBind();
}
[WebMethod]
public static string GetCustomers(string searchTerm, int pageIndex)
{
NorthwindEntities entities = new NorthwindEntities();
ObjectParameter recordCount = new ObjectParameter("RecordCount", typeof(int));
DataSet ds = new DataSet();
List<Customer> customers = entities.GetCustomersPageWise(searchTerm, pageIndex, PageSize, recordCount).ToList();
ds.Tables.Add(ToDataTable(customers, "Customers"));
DataTable dt = new DataTable("Pager");
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageSize");
dt.Columns.Add("RecordCount");
dt.Rows.Add();
dt.Rows[0]["PageIndex"] = pageIndex;
dt.Rows[0]["PageSize"] = PageSize;
dt.Rows[0]["RecordCount"] = Convert.ToInt32(recordCount.Value);
ds.Tables.Add(dt);
return ds.GetXml();
}
public static DataTable ToDataTable<T>(IEnumerable<T> collection, string name)
{
DataTable dt = new DataTable(name);
Type type = typeof(T);
PropertyInfo[] pia = type.GetProperties();
foreach (PropertyInfo pi in pia)
{
Type ColumnType = pi.PropertyType;
if ((ColumnType.IsGenericType))
{
ColumnType = ColumnType.GetGenericArguments()[0];
}
dt.Columns.Add(pi.Name, ColumnType);
}
foreach (T item in collection)
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
foreach (PropertyInfo pi in pia)
{
if (pi.GetValue(item, null) != null)
{
dr[pi.Name] = pi.GetValue(item, null);
}
}
dr.EndEdit();
dt.Rows.Add(dr);
}
return dt;
}
VB.Net
Private Shared PageSize As Integer = 5
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDummyRow()
End If
End Sub
Private Sub BindDummyRow()
Dim dummy As DataTable = New DataTable()
dummy.Columns.Add("CustomerID")
dummy.Columns.Add("ContactName")
dummy.Columns.Add("City")
dummy.Rows.Add()
gvCustomers.DataSource = dummy
gvCustomers.DataBind()
End Sub
<WebMethod()>
Public Shared Function GetCustomers(ByVal searchTerm As String, ByVal pageIndex As Integer) As String
Dim entities As NorthwindEntities = New NorthwindEntities()
Dim recordCount As ObjectParameter = New ObjectParameter("RecordCount", GetType(Integer))
Dim ds As DataSet = New DataSet()
Dim customers As List(Of Customer) = entities.GetCustomersPageWise(searchTerm, pageIndex, PageSize, recordCount).ToList()
ds.Tables.Add(ToDataTable(customers, "Customers"))
Dim dt As DataTable = New DataTable("Pager")
dt.Columns.Add("PageIndex")
dt.Columns.Add("PageSize")
dt.Columns.Add("RecordCount")
dt.Rows.Add()
dt.Rows(0)("PageIndex") = pageIndex
dt.Rows(0)("PageSize") = PageSize
dt.Rows(0)("RecordCount") = Convert.ToInt32(recordCount.Value)
ds.Tables.Add(dt)
Return ds.GetXml()
End Function
Public Shared Function ToDataTable(Of T)(ByVal collection As IEnumerable(Of T), ByVal name As String) As DataTable
Dim dt As DataTable = New DataTable(name)
Dim type As Type = GetType(T)
Dim pia As PropertyInfo() = type.GetProperties()
For Each pi As PropertyInfo In pia
Dim ColumnType As Type = pi.PropertyType
If (ColumnType.IsGenericType) Then
ColumnType = ColumnType.GetGenericArguments()(0)
End If
dt.Columns.Add(pi.Name, ColumnType)
Next
For Each item As T In collection
Dim dr As DataRow = dt.NewRow()
dr.BeginEdit()
For Each pi As PropertyInfo In pia
If pi.GetValue(item, Nothing) IsNot Nothing Then
dr(pi.Name) = pi.GetValue(item, Nothing)
End If
Next
dr.EndEdit()
dt.Rows.Add(dr)
Next
Return dt
End Function
Screenshot