Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 2-Apr-16 22:05pm
Comments
Kornfeld Eliyahu Peter 3-Apr-16 4:03am    
1. Please remove unrelated code
2. Format the rest so we can read it
3. Tell us what the error is!!!

1 solution

C#
CurrentPage = FillRepeater() – 1;

The minus sign looks suspicious to me; should be:
C#
CurrentPage = FillRepeater() - 1;
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 3-Apr-16 4:34am    
Do you mean, it looks more an em-dash or en-dash, than a simple minus sign? Good eyes!
What I wondering about is ,how one can enter such a sign in any IDE...
phil.o 3-Apr-16 5:12am    
Yes, I could not name the sign but there is a great chance it is an em- or en-dash. What I am sure of is, it is not a minus sign :)
Member 12170781 3-Apr-16 5:49am    
yes, You were right. The problem is with the minus sign only. I corrected my mistake...but the output I am getting is something that is not expected i.e last question of every page is display below the label.This happens every time when I move next,first,last and previous page..I don't know why
phil.o 3-Apr-16 5:53am    
Neither do I :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900