Hi lisamaclea,
Check this sample. now 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
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"
AllowPaging="true" OnPageIndexChanging="gvCustomers_PageIndexChanging" PageSize="10">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Label ID="lblCount" Text="" 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 Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindCustomers();
}
}
private void BindCustomers()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID,ContactName,City,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
int TotalRecords = ((System.Data.DataTable)gvCustomers.DataSource).Rows.Count;
int CurrentRecordStart = gvCustomers.PageIndex * gvCustomers.PageSize + 1;
int CurrentRecordEnd = gvCustomers.PageIndex * gvCustomers.PageSize + gvCustomers.Rows.Count;
lblCount.Text = string.Format("Displaying {0} to {1} of {2} records found", CurrentRecordStart, CurrentRecordEnd, TotalRecords);
}
}
}
}
protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
this.BindCustomers();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCustomers()
End If
End Sub
Private Sub BindCustomers()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerID,ContactName,City,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
Dim TotalRecords As Integer = (CType(gvCustomers.DataSource, System.Data.DataTable)).Rows.Count
Dim CurrentRecordStart As Integer = gvCustomers.PageIndex * gvCustomers.PageSize + 1
Dim CurrentRecordEnd As Integer = gvCustomers.PageIndex * gvCustomers.PageSize + gvCustomers.Rows.Count
lblCount.Text = String.Format("Displaying {0} to {1} of {2} records found", CurrentRecordStart, CurrentRecordEnd, TotalRecords)
End Using
End Using
End Using
End Sub
Protected Sub gvCustomers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
Me.BindCustomers()
End Sub
Screenshot