469,913 Members | 2,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

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.ArgumentOutOfRangeException: 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:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +7485640
System.Web.UI.WebControls.GridViewRowCollection.ge t_Item(Int32 index) +13
_Default.retrivefile(Int32 j) +977
_Default.Page_Load(Object sender, EventArgs e) +598
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e) +41
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2603
and the code that i used in retrivefile(int ) function is. Please check it..
Expand|Select|Wrap|Line Numbers
  1.  private void retrivefile(int j)
  2.     {
  3.  
  4.             string medium = Request.QueryString["medium"];
  5.  
  6.             ViewState["Question_selected"] = j;
  7.             string id = Session["uid"].ToString();
  8.             _repSource = new List<string>();
  9.             string url = Request.Url.ToString();
  10.             string[] url_array = url.Split('/');
  11.             string url1 = url_array[0] + "//" + url_array[2] + "/" + url_array[3];
  12.             SqlConnection filecon = new SqlConnection("Data Source="shekhar";initial catalog=walking;uid=sa;pwd=shekhar;");
  13.             filecon.Open();
  14.             SqlDataAdapter cmd = new SqlDataAdapter("select Serial_No from Question_Copy where Session_ID='" + id + "'", filecon);
  15.             DataSet ds = new DataSet();
  16.             cmd.Fill(ds);
  17.             int count = ds.Tables[0].Rows.Count;
  18.             string[] names = new string[count];
  19.             for (int i = 0; i < count; i++)
  20.             {
  21.                 names[i] = ds.Tables[0].Rows[i]["Serial_No"].ToString();
  22.  
  23.             }
  24.             ViewState["Quest_Srno"] = names.ToArray();
  25.  
  26.             j = selctquest(j);
  27.             string file1 = names[j] + ".mht";
  28.             if (medium == "English")
  29.             {
  30.                 _repSource.Insert(0, url1 + "/StaticHTM/" + file1);
  31.                 QuestionView1.DataSource = _repSource.ToArray();
  32.                 QuestionView1.DataBind();
  33.                 ViewState["Quest_Srno"] = names.ToArray();
  34.             }
  35.             if (medium == "Hindi")
  36.             {
  37.                 _repSource.Insert(0, url1 + "/New Folder/" + file1);
  38.                 QuestionView1.DataSource = _repSource.ToArray();
  39.                 QuestionView1.DataBind();
  40.                 ViewState["Quest_Srno"] = names.ToArray();
  41.  
  42.             }
  43.             Label question_index = (Label)QuestionView1.Rows[0].FindControl("Label6");
  44.             question_index.Text = "Question &nbsp; " + (j + 1) + "&nbsp; of &nbsp;" + 50;
  45.             int total_quest = (int)ViewState["total_quest"];
  46.             totalbutton(total_quest);
  47.             Button2.Enabled = true;
  48.             Button4.Enabled = true;
  49.             Session["disable_submit"] = j;
  50.  
  51.  
  52.     }

please help me out where i am doing mistake, waiting for your reply...........
Jan 4 '10 #1
6 18362
truezplaya
115 100+
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
Jan 4 '10 #2
@truezplaya
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.
Jan 6 '10 #3
Frinavale
9,735 Expert Mod 8TB
My guess would be on lines 26 and 27 in the above posted code:
Expand|Select|Wrap|Line Numbers
  1.   j = selctquest(j);
  2.   string file1 = names[j] + ".mht";
  3.  
What is j?
Is j larger than the number of string elements in the names array?

-Frinny
Jan 6 '10 #4
@Frinavale
thanx for your reply..
here ia m sending little section of gridview , i think it wll be helpfull to analyse my problem .
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="QuestionView1" runat="server" AutoGenerateColumns="False" 
  2.                           BackColor="#A0C0CF" BorderColor="#660033" 
  3.                           BorderStyle="Outset" Height="226px" PageSize="1" Visible="False" 
  4.                           Width="127px" AllowPaging="False">
  5.                           <PagerSettings NextPageText="" PreviousPageText="" Visible="False" />
  6.                           <Columns>
  7.  <asp:TemplateField>
  8.                                   <ItemTemplate>
  9.                                       &nbsp;&nbsp;<asp:Label ID="Label6" runat="server" Font-Bold="True"></asp:Label>
  10.                                       <iframe ID="iframe1" runat="server" height="250" scrolling="auto" 
  11.                                           src="<%# Container.DataItem %>" style="background-color: #99CCFF" width="600">
  12.                                       </iframe>
  13.                                       <asp:RadioButtonList ID="RadioButtonList1" runat="server" BorderColor="#33CCFF" 
  14.                                           RepeatDirection="Horizontal" TextAlign="Left" Width="585px" Font-Bold="True" ForeColor="#FFCC00">
  15.                                           <asp:ListItem Value="1">Answer 1</asp:ListItem>
  16.                                           <asp:ListItem Value="2">Answer 2</asp:ListItem>
  17.                                           <asp:ListItem Value="3">Answer 3</asp:ListItem>
  18.                                           <asp:ListItem Value="4">Answer 4</asp:ListItem>
  19.                                       </asp:RadioButtonList>
  20.                                   </ItemTemplate>
  21.                               </asp:TemplateField>
  22. </Columns>
  23. </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.
Expand|Select|Wrap|Line Numbers
  1. private int selctquest(int i)
  2. {
  3.  
  4.     string[] quest_id = (string[])ViewState["Quest_Srno"];
  5.     //_srno = new List<string>();
  6.     if (i >= quest_id.Length)
  7.     {
  8.         blank_file();
  9.         i = i + 1;
  10.     }
  11.     else
  12.     {
  13.  
  14.  
  15.     string tablename = (string)Session["uid"];
  16.     SqlConnection con = new SqlConnection("Data Source=Server;initial catalog=database;uid=sa;pwd=");
  17.     con.Open();
  18.  
  19.     SqlCommand cmd1 = new SqlCommand("select Count(*) from "+tablename+ "", con);
  20.     SqlDataReader reader = cmd1.ExecuteReader();
  21.     while (reader.Read())
  22.     {
  23.         int count = Convert.ToInt32(reader[0].ToString());
  24.         for (int j = 0; j <= count; j++)
  25.         {
  26.  
  27.             SqlConnection con1 = new SqlConnection("Data Source="Server";initial catalog=database;uid=sa;pwd=");
  28.             con1.Open();
  29.  
  30.             string sql = "select Q_ID from " + tablename + " ";
  31.             SqlCommand cmd = new SqlCommand(sql, con1);
  32.  
  33.             SqlDataReader dr = cmd.ExecuteReader();
  34.  
  35.             while (dr.Read())
  36.             {
  37.                 if (i > quest_id.Length-1)
  38.                 {
  39.                     i = i + 1;
  40.                     break;
  41.  
  42.                 }
  43.                 string matchquest = quest_id[i];
  44.                 int match_item = Convert.ToInt32(matchquest.ToString());
  45.                 if (match_item == Convert.ToInt32(dr[0].ToString()))
  46.                 {
  47.  
  48.  
  49.                     i = i + 1;
  50.  
  51.                 }
  52.  
  53.             }
  54.             con1.Close();
  55.             }
  56.         }
  57.     con.Close();
  58.     }
  59.     return i;
  60. }
Jan 7 '10 #5
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
Jan 8 '10 #6
Frinavale
9,735 Expert Mod 8TB
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
Jan 8 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by skura | last post: by
1 post views Thread by Anita | last post: by
2 posts views Thread by Toni | last post: by
16 posts views Thread by ES Kim | last post: by
8 posts views Thread by sam | last post: by
1 post views Thread by Steffen Stellwag | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.