Hello
guys sometimes you might have been faced the problem using grid view paging using
SQLDataReader. But the fact is you have
got the error like the data source does not support server-side data paging.
Yes it is true Gridview never support paging with DataReader.
You
cannot use paging with a DataReader. GridView paging only allow you if you fill
your gridview data using a Dataset or a Datatable using a DataAdapter.
.aspx
page
<asp:GridView ID="myGridView"
CellPadding="5"
Width="100%"
DataKeyNames="testID"
runat="server"
AutoGenerateColumns="false"
AllowPaging="true"
PageSize="5"
onpageindexchanging="myGridView_PageIndexChanging"
onrowdatabound="myGridView_RowDataBound">
<HeaderStyle BackColor="Aqua"
Font-Size="Medium"
ForeColor="Blue"
/>
<RowStyle BackColor="Brown"
Font-Size="Small"
ForeColor="GreenYellow"
/>
<AlternatingRowStyle
BackColor="Gray"
Font-Italic="true"/>
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name")
%>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Address/Feedback">
<ItemTemplate>
<asp:Label id="lblAddFeedback"
runat="server"
Text='<%#Eval("Address")
%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Feedback"
Visible="false">
<ItemTemplate>
<%#Eval("Feedback")%>
ItemTemplate>
asp:TemplateField>
Columns>
<EmptyDataTemplate>
No Record(s)
Found....
EmptyDataTemplate>
asp:GridView>
.aspx.cs page:
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Data.SqlClient;
public partial class GridView : System.Web.UI.Page
{
SqlConnection dbcon = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString());
clsDAL objDAL = new
clsDAL();
//Data Access Layer
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void
BindGridView()
{
string strQuery = "SELECT
Id, Name, Address, Feedback FROM Customer";
SqlCommand cmd = new
SqlCommand(strQuery, dbcon);
dbcon.Open();
SqlDataReader dr = cmd.ExecuteReader();
// SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new
DataTable();
// da.Fill(dt);
//myGridView.DataSource = dt;
//myGridView.DataBind();
// da.Dispose();
while (dr.Read())
{
myGridView.DataSource = dr;
myGridView.DataBind();
}
dt.Dispose();
cmd.Dispose();
dbcon.Close();
}
protected void
myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
BindGridView();
}
protected void
myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(myGridView.DataKeys[e.Row.RowIndex].Value);
Label lblDisp = (Label)e.Row.FindControl("lblAddFeedback");
lblDisp.Text= GetDisplayDetails(id);
}
}
protected string
GetDisplayDetails(int id)
{
string strQuery = "SELECT
Address FROM Customer WHERE Id = " + id + "";
SqlCommand cmd = new
SqlCommand(strQuery, dbcon);
// dbcon.Open();
SqlDataAdapter da = new
SqlDataAdapter(cmd);
DataTable dt = new
DataTable();
da.Fill(dt);
string Display = "";
if (dt.Rows.Count > 0)
{
Display =
dt.Rows[0]["Address"].ToString();
}
da.Dispose();
dt.Dispose();
cmd.Dispose();
//dbcon.Close();
return
Display;
}
}
Comments