Index was out of range. Must be non-negative and less than the size of the collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashi shekhar singh
    New Member
    • Aug 2009
    • 30

    Index was out of range. Must be non-negative and less than the size of the collection

    Respected Sir,
    I am facing problem when i try to deploy my website on iis 7.0 on test page.
    i have to display some .mht files on iframe in gridview and error looks like below,

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    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.Argument OutOfRangeExcep tion: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:


    [ArgumentOutOfRa ngeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index]
    System.Collecti ons.ArrayList.g et_Item(Int32 index) +7485640
    System.Web.UI.W ebControls.Grid ViewRowCollecti on.get_Item(Int 32 index) +13
    _Default.retriv efile(Int32 j) +977
    _Default.Page_L oad(Object sender, EventArgs e) +598
    System.Web.Util .CalliHelper.Ev entArgFunctionC aller(IntPtr fp, Object o, Object t, EventArgs e) +14
    System.Web.Util .CalliEventHand lerDelegateProx y.Callback(Obje ct sender, EventArgs e) +41
    System.Web.UI.C ontrol.OnLoad(E ventArgs e) +91
    System.Web.UI.C ontrol.LoadRecu rsive() +74
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +2603
    and the code that i used in retrivefile(int ) function is. Please check it..
    Code:
     private void retrivefile(int j)
        {
           
                string medium = Request.QueryString["medium"];
    
                ViewState["Question_selected"] = j;
                string id = Session["uid"].ToString();
                _repSource = new List<string>();
                string url = Request.Url.ToString();
                string[] url_array = url.Split('/');
                string url1 = url_array[0] + "//" + url_array[2] + "/" + url_array[3];
                SqlConnection filecon = new SqlConnection("Data Source="shekhar";initial catalog=walking;uid=sa;pwd=shekhar;");
                filecon.Open();
                SqlDataAdapter cmd = new SqlDataAdapter("select Serial_No from Question_Copy where Session_ID='" + id + "'", filecon);
                DataSet ds = new DataSet();
                cmd.Fill(ds);
                int count = ds.Tables[0].Rows.Count;
                string[] names = new string[count];
                for (int i = 0; i < count; i++)
                {
                    names[i] = ds.Tables[0].Rows[i]["Serial_No"].ToString();
    
                }
                ViewState["Quest_Srno"] = names.ToArray();
    
                j = selctquest(j);
                string file1 = names[j] + ".mht";
                if (medium == "English")
                {
                    _repSource.Insert(0, url1 + "/StaticHTM/" + file1);
                    QuestionView1.DataSource = _repSource.ToArray();
                    QuestionView1.DataBind();
                    ViewState["Quest_Srno"] = names.ToArray();
                }
                if (medium == "Hindi")
                {
                    _repSource.Insert(0, url1 + "/New Folder/" + file1);
                    QuestionView1.DataSource = _repSource.ToArray();
                    QuestionView1.DataBind();
                    ViewState["Quest_Srno"] = names.ToArray();
    
                }
                Label question_index = (Label)QuestionView1.Rows[0].FindControl("Label6");
                question_index.Text = "Question &nbsp; " + (j + 1) + "&nbsp; of &nbsp;" + 50;
                int total_quest = (int)ViewState["total_quest"];
                totalbutton(total_quest);
                Button2.Enabled = true;
                Button4.Enabled = true;
                Session["disable_submit"] = j;
    
            
        }

    please help me out where i am doing mistake, waiting for your reply.......... .
  • truezplaya
    New Member
    • Jul 2007
    • 115

    #2
    The way i find best to solve this sort of issue is to put a breakpoint on your private void retrivefile then you can step through and check how many values are within your collections and then check that value against the value that you are searching for.

    I'd be guessing but i would say it's either your _repsource or ds that do not contain the expected value.

    put the break point in and have aa look also check your for loop

    Hope this helps

    Regards
    Truez

    Comment

    • shashi shekhar singh
      New Member
      • Aug 2009
      • 30

      #3
      Originally posted by truezplaya
      The way i find best to solve this sort of issue is to put a breakpoint on your private void retrivefile then you can step through and check how many values are within your collections and then check that value against the value that you are searching for.

      I'd be guessing but i would say it's either your _repsource or ds that do not contain the expected value.

      put the break point in and have aa look also check your for loop

      Hope this helps

      Regards
      Truez
      Respected sir,
      The above code is working properly in Visual Studio 2008 Debug mode but when i deployed this project on IIS 7.0 then it gives an error like above shown so please help me out.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        My guess would be on lines 26 and 27 in the above posted code:
        Code:
          j = selctquest(j);
          string file1 = names[j] + ".mht";
        What is j?
        Is j larger than the number of string elements in the names array?

        -Frinny

        Comment

        • shashi shekhar singh
          New Member
          • Aug 2009
          • 30

          #5
          Originally posted by Frinavale
          My guess would be on lines 26 and 27 in the above posted code:
          Code:
            j = selctquest(j);
            string file1 = names[j] + ".mht";
          What is j?
          Is j larger than the number of string elements in the names array?

          -Frinny
          thanx for your reply..
          here ia m sending little section of gridview , i think it wll be helpfull to analyse my problem .
          Code:
          <asp:GridView ID="QuestionView1" runat="server" AutoGenerateColumns="False" 
                                    BackColor="#A0C0CF" BorderColor="#660033" 
                                    BorderStyle="Outset" Height="226px" PageSize="1" Visible="False" 
                                    Width="127px" AllowPaging="False">
                                    <PagerSettings NextPageText="" PreviousPageText="" Visible="False" />
                                    <Columns>
           <asp:TemplateField>
                                            <ItemTemplate>
                                                &nbsp;&nbsp;<asp:Label ID="Label6" runat="server" Font-Bold="True"></asp:Label>
                                                <iframe ID="iframe1" runat="server" height="250" scrolling="auto" 
                                                    src="<%# Container.DataItem %>" style="background-color: #99CCFF" width="600">
                                                </iframe>
                                                <asp:RadioButtonList ID="RadioButtonList1" runat="server" BorderColor="#33CCFF" 
                                                    RepeatDirection="Horizontal" TextAlign="Left" Width="585px" Font-Bold="True" ForeColor="#FFCC00">
                                                    <asp:ListItem Value="1">Answer 1</asp:ListItem>
                                                    <asp:ListItem Value="2">Answer 2</asp:ListItem>
                                                    <asp:ListItem Value="3">Answer 3</asp:ListItem>
                                                    <asp:ListItem Value="4">Answer 4</asp:ListItem>
                                                </asp:RadioButtonList>
                                            </ItemTemplate>
                                        </asp:TemplateField>
          </Columns>
          </asp:Gridview>
          and j=select(j) is a function which checks to know that any file have been accessed or not. i am sending you this function.
          Code:
          private int selctquest(int i)
          {
          
              string[] quest_id = (string[])ViewState["Quest_Srno"];
              //_srno = new List<string>();
              if (i >= quest_id.Length)
              {
                  blank_file();
                  i = i + 1;
              }
              else
              {
             
                  
              string tablename = (string)Session["uid"];
              SqlConnection con = new SqlConnection("Data Source=Server;initial catalog=database;uid=sa;pwd=");
              con.Open();
              
              SqlCommand cmd1 = new SqlCommand("select Count(*) from "+tablename+ "", con);
              SqlDataReader reader = cmd1.ExecuteReader();
              while (reader.Read())
              {
                  int count = Convert.ToInt32(reader[0].ToString());
                  for (int j = 0; j <= count; j++)
                  {
          
                      SqlConnection con1 = new SqlConnection("Data Source="Server";initial catalog=database;uid=sa;pwd=");
                      con1.Open();
                      
                      string sql = "select Q_ID from " + tablename + " ";
                      SqlCommand cmd = new SqlCommand(sql, con1);
          
                      SqlDataReader dr = cmd.ExecuteReader();
          
                      while (dr.Read())
                      {
                          if (i > quest_id.Length-1)
                          {
                              i = i + 1;
                              break;
                          
                          }
                          string matchquest = quest_id[i];
                          int match_item = Convert.ToInt32(matchquest.ToString());
                          if (match_item == Convert.ToInt32(dr[0].ToString()))
                          {
                             
                              
                              i = i + 1;
          
                          }
          
                      }
                      con1.Close();
                      }
                  }
              con.Close();
              }
              return i;
          }

          Comment

          • shashi shekhar singh
            New Member
            • Aug 2009
            • 30

            #6
            Thanx a lot for your help , i heave solved my problem.

            but another problem occurs , when i try to access the files from the browser directly without going to login page on iis 7.0 then files are shown , Do you have any idea to restrict these files directly from the browser, i want to access these files only viewable on my test page in iframe and nothing anywhere else.
            is it possible by adding manged handler in iis 7.0. if you have any other idea then please help me out.. i do'nt want to encrypt these all files or by adding some sripts to the every file(.mht) to embed in test.aspx page. please help me out it's urgent for me...i am waiting for you reply.....
            from ..
            shashi shekhar singh

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I'm glad you solved your problem. Please do not post post new questions in the same thread as your old question. Please post new questions in a new thread.

              You should probably research Forms Authentication. This will let you restrict access to resources that are not meant for the public eye.

              -Frinny

              Comment

              Working...