Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Argument out of range exception

Argument out of range exception

Scheduled Pinned Locked Moved ASP.NET
helpdatabasedesignalgorithmsquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Greetings experts, I am incredibly frustrated by this task. We have a sorting feature added to our GridView control which works great. Then we decided to adding sorting up and down arrow and that's when we started having issues. I have been working on this now for three days and can't seem to get anywhere with it. I really do need some help. When we run our code, we get keep getting the following error: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' The error is on this line:

    string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells[i].Controls[0]).Text;

    I believe this is the relevant code:

    protected void OnSorting(object sender, GridViewSortEventArgs e)
    {
        string SortDir = string.Empty;
        DataTable dt = new DataTable();
        dt = ViewState\["tables"\] as DataTable;
        {
            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                SortDir = "Desc";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                SortDir = "Asc";
            }
            DataView sortedView = new DataView(dt);
            sortedView.Sort = string.Format("{0} {1}", e.SortExpression, SortDir);
            grvSpeakers.DataSource = sortedView;
            grvSpeakers.DataBind();
            for (int i = 0; i < grvSpeakers.Columns.Count; i++)
            {
                string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text;
                if (lbText == e.SortExpression)
                {
                    TableCell tableCell = grvSpeakers.HeaderRow.Cells\[i\];
                    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
                    img.ImageUrl = (SortDir == "Asc") ? "~/Images/down\_arrow.png" : "~/Images/up\_arrow.png";
                    tableCell.Controls.Add(new LiteralControl(" "));
                    tableCell.Controls.Add(img);
                }
            }
        }
    }
    

    Please forgive me for the code dump. I am hoping it helps show what I could be doing wrong. The GridView has 6 header columns with SortExpressions, Speaker Name, Ministry Name, Email, Date Added, Client Name and website URL Each has a sortExpress like

    SortExpression="SpeakerName"

    for instance. Any assistance is greatly apprecia

    L 1 Reply Last reply
    0
    • S samflex

      Greetings experts, I am incredibly frustrated by this task. We have a sorting feature added to our GridView control which works great. Then we decided to adding sorting up and down arrow and that's when we started having issues. I have been working on this now for three days and can't seem to get anywhere with it. I really do need some help. When we run our code, we get keep getting the following error: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' The error is on this line:

      string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells[i].Controls[0]).Text;

      I believe this is the relevant code:

      protected void OnSorting(object sender, GridViewSortEventArgs e)
      {
          string SortDir = string.Empty;
          DataTable dt = new DataTable();
          dt = ViewState\["tables"\] as DataTable;
          {
              if (SortDirection == SortDirection.Ascending)
              {
                  SortDirection = SortDirection.Descending;
                  SortDir = "Desc";
              }
              else
              {
                  SortDirection = SortDirection.Ascending;
                  SortDir = "Asc";
              }
              DataView sortedView = new DataView(dt);
              sortedView.Sort = string.Format("{0} {1}", e.SortExpression, SortDir);
              grvSpeakers.DataSource = sortedView;
              grvSpeakers.DataBind();
              for (int i = 0; i < grvSpeakers.Columns.Count; i++)
              {
                  string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text;
                  if (lbText == e.SortExpression)
                  {
                      TableCell tableCell = grvSpeakers.HeaderRow.Cells\[i\];
                      System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
                      img.ImageUrl = (SortDir == "Asc") ? "~/Images/down\_arrow.png" : "~/Images/up\_arrow.png";
                      tableCell.Controls.Add(new LiteralControl(" "));
                      tableCell.Controls.Add(img);
                  }
              }
          }
      }
      

      Please forgive me for the code dump. I am hoping it helps show what I could be doing wrong. The GridView has 6 header columns with SortExpressions, Speaker Name, Ministry Name, Email, Date Added, Client Name and website URL Each has a sortExpress like

      SortExpression="SpeakerName"

      for instance. Any assistance is greatly apprecia

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2
              string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text;
      

      The message suggests that the i index is invalid for the number of cells in the row, or Controls does not contain any items. You need to use the debugger to find out which one is the problem.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups