数据列表分页错误 [英] datalist paging error

查看:130
本文介绍了数据列表分页错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

在我的项目中,每个人都可以发送其链接.但是在批准后,用户可以在DataList中看到链接.我已在DataList中使用分页.
并根据已批准的状态检索数据,但显示错误.

代码是

Hi all

In my project everyone can send their link. But after approval the user can see the link in DataList. I have used paging in DataList.
and retrieved the data according to status which are approved but it is showing me an error.

code is

private int RowCount
    {
        get { return Convert.ToInt32(ViewState["RowCount"]); }
        set { ViewState["RowCount"] = value; }
    }

    private void FetchData(int take, int pageSize)
    {
        using (myClassesDataContext dc = new myClassesDataContext ())
        {
            var  idata =  from link in dc.links
                             where link.Status == "Approved"
                             select new  
                             {
                                 Count = dc.links.Count()
                             };
                       
            if (idata.DefaultIfEmpty () ==null )
            {
                lblPageName.ForeColor = System.Drawing.Color.Red;
                lblPageName.Text = "Sorry!!!! No data is Available Now.";

            }
            else
            {
                var query = from p in dc.links
                           .Take(take)
                           .Skip(pageSize)
                            orderby p.SubmittedDate descending 
                            where p.Status =="Approved"
                           select new
                            {
                                p.Link_ID ,
                                title = p.Title,
                                url = p.Url,
                                keyword = p.Keyword,
                                description = p.Descriptions,
                                email = p.Email,
                                contactname = p.ContactName,
                                p.SubmittedDate,
                                Count = dc.Backlinks.Count()
                            };

                PagedDataSource page = new PagedDataSource();
                page.AllowCustomPaging = true;
                page.AllowPaging = true;
                page.DataSource = query;
                page.PageSize = 50;
                DataList1.DataSource = page;
                DataList1.DataBind();

                if (!IsPostBack)
                {
                    RowCount = query.First().Count;
                    CreatePagingControl();
                }
            }
        }
    }



错误消息如下.



The error message is below.

Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 95:                 if (!IsPostBack)
Line 96:                 {
Line 97:                     RowCount = query.First().Count;
Line 98:                     CreatePagingControl();
Line 99:                 }



代码有什么问题?请帮忙.
预先感谢.



What''s wrong in the code? Please help.
Thanks in advance.

推荐答案

使用"Count"方法检查记录是否存在.
Check whether the record exists or not by using the "Count" method.
int recCount = MyContext.MyTable.Count(elem => elem.ID == ID);
if (recCount > 0)
{
TableObject tblObject = (TableObject) MyContext.MyTable.SingleOrDefault(elem =>; elem.ID == ID)
}



这会给您一个想法.



This will give you an idea.


Starlene,


良好的编码,您将保持良好的编码模式,这是我希望从您的问题中得到的解决方案.

在查询中,您首先执行了"Take()"函数,然后又执行了"Skip()"函数,但是在代码中,执行"Take"和"Skip"后的顺序是错误的,因此请在第一次编写时检查代码跳过然后转到Take功能.
Hi Starlene,


Nice coding and you are maintaining good coding patteren and here is the solution i am expecting from your problem.

in the query you taken first ''Take()'' function and then you taken the ''Skip()'' function, the order you followed for Take and Skip is only wrong in your code so please chcek the code as first write skip and then go for Take function.
<br />
<pre>Yours original code:<br />
                var query = from p in dc.links<br />
                           .Take(take)<br />
                           .Skip(pageSize)<br />
                            orderby p.SubmittedDate descending <br />
                            where p.Status =="Approved"<br />
                           select new<br />
                            {<br />
                                p.Link_ID ,<br />
                                title = p.Title,<br />
                                url = p.Url,<br />
                                keyword = p.Keyword,<br />
                                description = p.Descriptions,<br />
                                email = p.Email,<br />
                                contactname = p.ContactName,<br />
                                p.SubmittedDate,<br />
                                Count = dc.Backlinks.Count()<br />
                            };<br />
<br />
Corrected code :<br />
                var query = from p in dc.links<br />
                           .Skip(pageSize)<br />
                           .Take(take)<br />
                            orderby p.SubmittedDate descending <br />
                            where p.Status =="Approved"<br />
                           select new<br />
                            {<br />
                                p.Link_ID ,<br />
                                title = p.Title,<br />
                                url = p.Url,<br />
                                keyword = p.Keyword,<br />
                                description = p.Descriptions,<br />
                                email = p.Email,<br />
                                contactname = p.ContactName,<br />
                                p.SubmittedDate,<br />
                                Count = dc.Backlinks.Count()<br />
                            };</pre><br />


跳过和获取功能的说明如下
跳过:-绕过序列中指定数量的元素,然后返回其余元素.
取:-从序列的开头返回指定数量的连续元素.
当您第一次使用时,它首先需要n个记录,然后跳过By传递n个记录,然后我们在结果查询"中找不到任何记录,然后您请求从此计数,所以它抛出了错误消息.
RowCount = query.First().Count;

我们什么也做不了"

如果上述解决方案不起作用,请向您回复此新问题或错误

感谢和问候,
SivaRamaKrishnaRaju ..


Description for Skip and Take function is as follows
Skip:-Bypasses a specified number of elements in a sequence and then returns the remaining elements.
Take:- Returns a specified number of contiguous elements from the start of a sequence.
when you are using first it takes n number of records first and then skip By passes n no of records then we cann''t find no record in the resultant ''query'' then you are requesting count form this, so it is throwing an error message.
RowCount = query.First().Count;

"We can''t get any thing from nothing"

if the above solution is not works please reply to this post with you new problem or error

Thanks and Regards,
SivaRamaKrishnaRaju..


这篇关于数据列表分页错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆