none
LINQ's Got Error RRS feed

  • Question

  • Hey everyone,

    I was trying to achieve custom data paging by using LINQ  but LINQ has got some issues. Ok let me introduce what i've done and got back. There are 2 dropdownlists and listbox on my page. one of dropdownlists is for Page Number and the other is for Page size and the results show in ListBox.

    Page number is changing according to selected size at dropdownlist which is for page size. 

    and the related code is here :  

       

    Code Snippet

    private void FindPageTotalNumber()
    {
    ddPageIndex.Items.Clear();
    UsersDataContext tu = new UsersDataContext();
    var allusersNumbers = from users in tu.Kullanicis
    select new { users.KullaniciID };
    int selectedPageSize = Convert.ToInt32(ddPageSize.SelectedItem.Text);
    int count = allusersNumbers.Count();
    int totalPageSize = 0;
    if (count % selectedPageSize == 0)
    {
    totalPageSize = count / selectedPageSize;
    }
    else
    {
    totalPageSize = (count / selectedPageSize) + 1;
    }
    for (int i = 1; i <= totalPageSize; i++)
    {
    ddPageIndex.Items.Add(i.ToString());
    }
    }

     
    Now here is the DataBind Code to bind related data to ListBox after changing page number or page size :
     
       
    Code Snippet

    private void BindDataToUsersListBox()
    {
    UsersDataContext u = new UsersDataContext();

    var users = from au in u.Kullanicis
    select new { au.KullaniciAdi, au.KullaniciID };

    int pageIndex = Convert.ToInt32(ddPageIndex.SelectedItem.Text);
    int pageSize= Convert.ToInt32(ddPageSize.SelectedItem.Text);

    lbKullanicilar.DataSource = users.Skip((Convert.ToInt32(pageIndex)-1)*pageSize).Take(pageSize);

    lbKullanicilar.DataBind();
    }

    The other Stuff :
        
    Code Snippet

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FindPageTotalNumber();
    BindDataToUsersListBox();
    }
    } }

       protected void ddPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
    FindPageTotalNumber();
    BindDataToUsersListBox();
    }
    protected void ddPageIndex_SelectedIndexChanged(object sender, EventArgs e)
    {
    BindDataToUsersListBox();
     
      Ok now when I select page size 1, naturally page index is increasing and should show data one by one. 
    There is no problem here but when I select page number 1 it shows the first record and when i select number 2 it still shows the first record and when I select 3th page it shows me fourth record and when I select 4th page it shows me nothing !
    And I am going crazy cuz it took my whole day !
     
    thanks in advance... 
     
    Some Screen Shot may help you to see what the problem is : 
    My database :
     
    Screen Shot 1 :

     
    Screen Shot 2 :

     

     Screen Shot 3 :


    Screen Shot 4 :


    Screen Shot 5:

     

    Tuesday, April 8, 2008 9:02 PM

Answers

  • I'm not sure if this is your problem, however, you need to give the query an ordering with an orderby clause before you can expect to get consistent results with Skip & Take.

     

    Wednesday, April 9, 2008 7:06 AM
    Moderator

All replies

  • I'm not sure if this is your problem, however, you need to give the query an ordering with an orderby clause before you can expect to get consistent results with Skip & Take.

     

    Wednesday, April 9, 2008 7:06 AM
    Moderator
  •  Matt Warren - MSFT wrote:
    I'm not sure if this is your problem, however, you need to give the query an ordering with an orderby clause before you can expect to get consistent results with Skip & Take.

     



    When I give consistent values for take and skip, it works fine but if the values like that written above, it doesnt work fine.. Sad
    Wednesday, April 9, 2008 7:40 AM
  •  Matt Warren - MSFT wrote:
    I'm not sure if this is your problem, however, you need to give the query an ordering with an orderby clause before you can expect to get consistent results with Skip & Take.

     



    And umm I've never thought like that. I'll try it immediately.but some of my friends said me that the problem can persist due to DropDownList viewstate. Is that possible ?
    Wednesday, April 9, 2008 7:44 AM