Click here to Skip to main content
16,012,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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.
Posted
Updated 11-Jan-11 18:23pm
v2
Comments
Ankur\m/ 12-Jan-11 0:27am    
It's seems to be a LINQ related error. Google for "Sequence contains no elements" and check out the links. That should help you.

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.
 
Share this answer
 
Comments
Ankur\m/ 12-Jan-11 0:32am    
Or this: http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/527391ea-c0f4-488d-b0d2-94a11f483179/#54dde212-013d-4d2c-bb3e-72296a237ebc
Starlene 12-Jan-11 0:35am    
thank for post .now code is working.
Kasson 12-Jan-11 0:36am    
Thanks Starlene
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.
Yours original code:
                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()
                            };

Corrected code :
                var query = from p in dc.links
                           .Skip(pageSize)
                           .Take(take)
                            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()
                            };

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..
 
Share this answer
 
Comments
Starlene 13-Jan-11 8:47am    
thanks for help.

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