The error is in first statement of last method of this code i.e. "CurrentPage = FillRepeater() – 1;" For paging purpose we will use PagedDataSource class which is the same class used for paging in other databound controls.In FillRepeater() I created the object of the PagedDataSource class and utilizes some of its methods.After this simple property used to get the current page value as follows. This simply uses the viewstate to store the current state of the Data page. And also I will add functionality to linkbutton which gets First, Last, Previous and Next page on click event as shown below.
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class ExamPage : System.Web.UI.Page
{
SqlConnection con;
string query;
public ExamPage()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private int FillRepeater()
{
query = "select top 10 Question,Option1,Option2,Option3,Option4 from Questions";
SqlCommand cmd = new SqlCommand(query, con);
con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true; pds.PageSize = 8;
int count = pds.PageCount;
pds.CurrentPageIndex = CurrentPage;
if (pds.Count > 0)
{
lbtnPrev.Visible = true;
lbtnNext.Visible = true;
lbtnFirst.Visible = true;
lbtnLast.Visible = true;
lblStatus.Text = "Page " + Convert.ToString(CurrentPage + 1) + "of" + Convert.ToString(pds.PageCount);
}
else {
lbtnPrev.Visible = false;
lbtnNext.Visible = false;
lbtnFirst.Visible = false;
lbtnLast.Visible = false;
}
lbtnPrev.Enabled = !pds.IsFirstPage;
lbtnNext.Enabled = !pds.IsLastPage;
lbtnFirst.Enabled = !pds.IsFirstPage;
lbtnLast.Enabled = !pds.IsLastPage;
Repeater1.DataSource = pds;
Repeater1.DataBind();
return count;
}
public int CurrentPage
{
get
{
object obj = this.ViewState["_CurrentPage"]; if (obj == null) { return 0; }
else
{
return (int)obj;
}
}
set
{
//set in viewstate the current page number
this.ViewState["_CurrentPage"] = value;
}
}
protected void lbtnPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
FillRepeater();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
FillRepeater();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
FillRepeater();
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = FillRepeater() – 1;
FillRepeater();
}
}
I am getting syntax error not run-time error but I don't know how to solve it. Please tell me where I am making mistake