Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
0
Answer
resolve index -5 is either negative or above rows count
Emeka Okoye
7y
196
1
Reply
Please how any I resolve this issue?
I am trying to modify my datalist view to have paging , I used the below code which I found on c-sharpconer, but I get error message when I click on the previous buttons, all other buttons work work.
The error message says:
Index -5 is either negative or above rows count.
this is the aspx
<%@ Page Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"WebForm1.aspx.cs"
Inherits=
"PagininDataList.WebForm1"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<head runat=
"server"
>
<title></title>
<style type=
"text/css"
>
.style1
{
width: 672px;
}
</style>
</head>
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<table style=
"height: 79px; width: 429px"
>
<asp:DataList ID=
"DataList1"
runat=
"server"
>
<HeaderTemplate>
<h1> Details </h1>
</HeaderTemplate>
<ItemTemplate>
<tr>
My Custom Datalist Template here
</tr>
</ItemTemplate>
</asp:DataList>
</table>
<table>
<tr>
<td>
<asp:Button ID=
"btnfirst"
runat=
"server"
Font-Bold=
"true"
Text=
"<<"
Height=
"31px"
Width=
"43px"
onclick=
"btnfirst_Click"
/></td>
<td>
<asp:Button ID=
"btnprevious"
runat=
"server"
Font-Bold=
"true"
Text=
"<"
Height=
"31px"
Width=
"43px"
onclick=
"btnprevious_Click"
/></td>
<td>
<asp:Button ID=
"btnnext"
runat=
"server"
Font-Bold=
"true"
Text=
">"
Height=
"31px"
Width=
"43px"
onclick=
"btnnext_Click"
/></td>
<td>
<asp:Button ID=
"btnlast"
runat=
"server"
Font-Bold=
"true"
Text=
">>"
Height=
"31px"
Width=
"43px"
onclick=
"btnlast_Click"
/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
This is the C# Code
namespace
PagininDataList
{
public
partial
class
WebForm1 : System.Web.UI.Page
{
SqlDataAdapter dadapter;
DataSet dset;
PagedDataSource adsource =
new
PagedDataSource();
string
connstring =
"My connection string"
;
int
pos;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
this
.ViewState[
"vs"
] = 0;
}
pos = (
int
)
this
.ViewState[
"vs"
];
databind();
}
public
void
databind()
{
dadapter =
new
SqlDataAdapter(
"my SELECT query"
, connstring);
dset =
new
DataSet();
dadapter.Fill(dset);
adsource.DataSource = dset.Tables[0].DefaultView;
adsource.PageSize = 3;
adsource.AllowPaging=
true
;
adsource.CurrentPageIndex = pos;
btnfirst.Enabled = !adsource.IsFirstPage;
btnprevious.Enabled = !adsource.IsFirstPage;
btnlast.Enabled = !adsource.IsLastPage;
btnnext.Enabled = !adsource.IsLastPage;
DataList1.DataSource = adsource;
DataList1.DataBind();
}
protected
void
btnfirst_Click(
object
sender, EventArgs e)
{
pos = 0;
this
.ViewState[
"vs"
] = 0;
databind();
}
protected
void
btnprevious_Click(
object
sender, EventArgs e)
{
pos = (
int
)
this
.ViewState[
"vs"
];
pos -= 1;
this
.ViewState[
"vs"
] = pos;
databind();
}
protected
void
btnnext_Click(
object
sender, EventArgs e)
{
pos = (
int
)
this
.ViewState[
"vs"
];
pos += 1;
this
.ViewState[
"vs"
] = pos;
databind();
}
protected
void
btnlast_Click(
object
sender, EventArgs e)
{
pos = adsource.PageCount - 1;
this
.ViewState[
"vs"
] = pos;
databind();
}
}
}
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
How to Show Notification Messages
authorization, resource and client application - oauth2