By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

ArgumentOutOfRangeException was unhandled by user code

P: 2
The error I get is

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

This points to

Expand|Select|Wrap|Line Numbers
  1.  using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12.  
  13. public partial class ResHomePage : System.Web.UI.Page
  14. {
  15.     SqlConnection cn;
  16.     SqlCommand cmd, cmd2;
  17.     SqlDataReader dr;
  18.     string d, sd;    
  19.     Rest_RegisterClass ss;
  20.     int a, b, m, sum, f;
  21.     DataView dv = new DataView();
  22.     DataRow rec;
  23.     DataTable dt = new DataTable();
  24.     DataColumn col;
  25.     CheckBox c;
  26.  
  27.     protected void Page_Load(object sender, EventArgs e)
  28.     {
  29.         NewConnection();
  30.         sd = Session["resname"].ToString();
  31.         Label2.Text = sd;
  32.         d = Request["a"];
  33.         Label4.Text = DateTime.Now.ToLongTimeString();
  34.  
  35.         Label24.Text ="," + DateTime.Now.ToLongDateString();
  36.         //Label4.Text = Format(Now, "dddd, dd-MMM-yyyy<br>hh:mmtt");
  37.         //Label24.Text = Format(Now, "dddd");
  38.         ss = new Rest_RegisterClass();
  39.         ss.RestId = d;
  40.         lad.Text = ss.RAddress1 + ", " + ss.RAddress2 ;
  41.         lc.Text = ss.RCity;
  42.         ls.Text = ss.RState;
  43.         lz.Text = ss.RZipCode;
  44.         lph.Text = ss.RPhone;
  45.         Lot.Text = ss.ROTime;
  46.         lct.Text = ss.RCTime;
  47.  
  48.  
  49.         MenuClass obj = new MenuClass();
  50.         obj.RestId = d;
  51.         obj.Retrivedealb();
  52.         obj.retrivedeall();
  53.         obj.retrivedeald();
  54.  
  55.         Label24.Text = obj.DayOfW;
  56.  
  57.         Label27.Text = obj.StartTimeB;
  58.         Label28.Text = obj.EndTimeB;
  59.         Label29.Text = obj.StartTimeL;
  60.         Label30.Text = obj.EndTimeL;
  61.         Label31.Text = obj.StartTimeD;
  62.         Label32.Text = obj.EndTimeD;
  63.         cmd = new SqlCommand("select * from restaurant where restaurantid='" + d + "'", cn);
  64.         dr = cmd.ExecuteReader();
  65.         dr.Read();
  66.         img2.ImageUrl = dr["headerimage"].ToString();
  67.         img1.ImageUrl = dr["logo"].ToString();
  68.         dr.Close();
  69.         if (IsPostBack)
  70.         {
  71.             col = new DataColumn("itemno");
  72.             dt.Columns.Add(col);
  73.             col = new DataColumn("itemname");
  74.             dt.Columns.Add(col);
  75.             col = new DataColumn("qty");
  76.             dt.Columns.Add(col);
  77.             col = new DataColumn("itemprice");
  78.             dt.Columns.Add(col);
  79.  
  80.             Cache["Tapas"] = dt;
  81.         }
  82.         Session["total"] = Label19.Text;
  83.     }
  84.     public void NewConnection()
  85.     {
  86.         cn = new SqlConnection(clsConnectionString.StrCon);
  87.         cn.Open();
  88.     }
  89.     protected void LinkButton8_Click(object sender, EventArgs e)
  90.     {
  91.         Panel1.Visible = true;
  92.     }
  93.     protected void LinkButton9_Click(object sender, EventArgs e)
  94.     {
  95.         Response.Redirect("SpMenu.aspx?a=" + Session["id11"]);
  96.     }
  97.     protected void mil1_Click(object sender, EventArgs e)
  98.     {
  99.         f = 0;
  100.         Label16.Visible = false;
  101.         Label17.Visible = false;
  102.         listb();
  103.     }
  104.     private ArrayList Timings(string mid)
  105.     {
  106.  
  107.         NewConnection();
  108.         string s = "select StartTime,EndTime from menuavailability where description='" + mid + "' and DayofWeek='" + DateTime.Today.DayOfWeek.ToString() + "'";
  109.         cmd = new SqlCommand(s, cn);
  110.         dr = cmd.ExecuteReader();
  111.         ArrayList a = new ArrayList();
  112.         while (dr.Read())
  113.         {
  114.             a.Add(dr[0]);
  115.             a.Add(dr[1]);
  116.         }
  117.         dr.Close();
  118.         return a;
  119.     }
Expand|Select|Wrap|Line Numbers
  1.  private void listb()
  2.     {
  3.         Label36.Visible = true;
  4.         string str = "breakfast";
  5.         ArrayList ar = Timings(str);
  6.         DateTime t, t1, t2;
  7.         t = DateTime.Now;
  8.         t1 = (DateTime)(ar[0])/I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
  9. [/b][/i]
  10.         t2 = (DateTime)(ar[1]);
  11.         double d = (t.Hour * 60) + t.Minute;
  12.         double stime = (t1.Hour * 60) + t1.Minute;
  13.         double etime = (t2.Hour * 60) + t2.Minute;
  14.         if (d < etime)
  15.         {
  16.             Label36.Text = "";
  17.             Label14.Visible = true;
  18.             MIB.Visible = true;
  19.             databreak();
  20.         }
  21.         else
  22.         {
  23.             Label36.Text = "Breakfast time over so not available";
  24.             MIB.Visible = false;
  25.             Label14.Visible = false;
  26.         }
  27.     }
  28.  
  29.     private void listl()
  30.     {
  31.         Label36.Visible = true;
  32.         string str = "lunch";
  33.         ArrayList ar = Timings(str);
  34.         DateTime t, t1, t2;
  35.         t = DateTime.Now;
  36.  
  37.         t1 = (DateTime)(ar[0]);//I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
  38.         t2 = (DateTime)(ar[1]);
  39.  
  40.         double d = (t.Hour * 60) + t.Minute;
  41.         double stime = (t1.Hour * 60) + t1.Minute;
  42.         double etime = (t2.Hour * 60) + t2.Minute;
  43.         if (d < etime)
  44.         {
  45.             Label36.Text = "";
  46.             MIB.Visible = true;
  47.             Label6.Visible = true;
  48.  
  49.             datalunch();
  50.         }
  51.         else
  52.         {
  53.             Label36.Text = "Lunch time over so not available";
  54.             MIB.Visible = false;
  55.             Label6.Visible = false;
  56.         }
  57.     }
  58.  
  59.     private void listd()
  60.     {
  61.         Label36.Visible = true;
  62.         string str = "dinner";
  63.         ArrayList ar = Timings(str);
  64.         DateTime t, t1, t2;
  65.         t = DateTime.Now;
  66.         t1 = (DateTime)(ar[0]);//I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
  67. [/b][/i]        t2 = (DateTime)(ar[1]);
  68.         double d = (t.Hour * 60) + t.Minute;
  69.         double stime = (t1.Hour * 60) + t1.Minute;
  70.         double etime = (t2.Hour * 60) + t2.Minute;
  71.         if (d < etime)
  72.         {
  73.             MIB.Visible = true;
  74.             Label36.Text = "";
  75.             Label7.Visible = true;
  76.             datadinner();
  77.         }
  78.         else
  79.         {
  80.             Label36.Text = "Dinner not available";
  81.             MIB.Visible = false;
  82.             Label7.Visible = false;
  83.         }
  84.     }
  85.  
  86.     private void  fun1()
  87.     {
  88.         dt =(DataTable)(Cache["Tapas"]);
  89.  
  90.         string item1,item2;
  91.         int flag= 0;
  92.  
  93.             foreach(DataGridItem i in MIB.Items)
  94.             {
  95.                 c =(CheckBox )(i.FindControl("chk"));
  96.                 if (c.Checked == true)
  97.                 {
  98.                     if(dt.Rows.Count == 0)
  99.                     {
  100.                         rec = dt.NewRow();
  101.                         rec[0] = i.Cells[1].Text;
  102.                         rec[1] = i.Cells[2].Text;
  103.                         a = (int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text);
  104.                         rec[2] = a ;//qty
  105.                         b = (int.Parse)(i.Cells[4].Text);  //Item price
  106.                         dt.Rows.Add(rec);
  107.                         if (a >= 1) 
  108.                         {
  109.                             rec[3] = b * a;      //Total price
  110.                             m = a * b;
  111.                         }
  112.                         else
  113.                         {
  114.                             rec[3] = b * 1;
  115.                             m = 1 * b;
  116.                         }
  117.                     } 
  118.                     else
  119.                         {
  120.                            foreach(DataRow drw in dt.Rows)
  121.                            {
  122.                             item1 = drw["itemno"].ToString();
  123.                             item2 = i.Cells[1].Text;
  124.                             if(item1 == item2)
  125.                             {
  126.                                 drw["qty"] = ((int.Parse)(drw["qty"].ToString()))+ ((int.Parse)(((TextBox)(i.Cells[5].Controls[1] )).Text));
  127.                                 b =((int.Parse) (i.Cells[4].Text));  //item price
  128.                                 a = ((int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text));
  129.                                 drw["itemprice"] = ((int.Parse)(drw["itemprice"].ToString())) + (b * a);
  130.                                 flag = 1;
  131.                             }
  132.                            }
  133.                            if (flag == 0) 
  134.                            {
  135.                             rec = dt.NewRow();
  136.                             rec[0] = i.Cells[1].Text;    //item no
  137.                             rec[1] = i.Cells[2].Text;    //item name
  138.                             b =((int.Parse)( i.Cells[4].Text));  //item price
  139.                             a = ((int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text));
  140.                             rec[2] = a;
  141.                             //b=qty
  142.                             rec[3] = a * b;
  143.                             m = a * b;
  144.                             dt.Rows.Add(rec);
  145.                        }
  146.                     }
  147.                 }
  148.             }
  149.                 foreach(DataRow drw in dt.Rows)
  150.                 {
  151.                 sum = sum + ((int.Parse)(drw["itemprice"].ToString()));
  152.                 Label19.Text = sum.ToString();
  153.                 }
  154.             Label7.Text = "";
  155.             DataGrid1.Visible = true;
  156.             DataGrid1.DataSource = dt;
  157.             Cache["Tapas"] = dt;
  158.             DataGrid1.DataBind();
  159.             Cache["ItemAdded"] = dt;
  160.  
  161.  
  162.     }
  163.  
  164.  
This is the last error i need to solve to get my complete application to work again but it all points to 1 webpage and i get different errors if i try different things so i dont now anymore what the best way is
please someone help me i only need to solve this page error so my complete application works.

i tried to put in different values but it shows the same error.

You can see the whole program in the attachment.

Could someone help me with this error I will be really thankful to you.please write the changed code.
I get the errors in line 8,37,67.
May 6 '10 #1
Share this Question
Share on Google+
5 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Visual Studio stops on the line that has the problem.

Look at the Autos and Locals pallets. They will tell you the values of your variables.

The error message is saying to your trying to do something beyond the range of the collection.

For example, you are trying to get the 10 index of a list with only 5 items.
you are trying to get the characters 15-20 of a string with only 17 letters.
you are trying to get menu item -17 when you can only have positive numbers (or -1 for no item selected)

Just debug again, only this time when it stops on one of your lines check carefully all of your variables.
May 6 '10 #2

P: 2
@tlhintoq
Can you please write in the code for the error .
Still Im getting the same error even after I checked all the variables and debugged the program.
May 6 '10 #3

tlhintoq
Expert 2.5K+
P: 3,525
Can you please write in the code for the error
That doesn't even make sense. Not in English anyway.

I have no idea what line of your program is causing the error because you never said. I'm not going to download your entire application and do all of your debugging for you.

At least narrow down the search. What line has the error and what are all the variables and values at the time of the error?
May 6 '10 #4

tlhintoq
Expert 2.5K+
P: 3,525
Expand|Select|Wrap|Line Numbers
  1.  t = DateTime.Now;
  2.         t1 = (DateTime)(ar[0])/I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
  3. [/b][/i]
  4.         t2 = (DateTime)(ar[1]);
Line 2 won't compile like that - so I don't think you are showing the code as you really have it in your program. Either that or you really haven't tried to debug it yourself and just want someone else to do the work for you.

You are not doing any range checking. You are just assuming you actually have an index 0 and index 1 in your ar[] array. If you never initialized the array with values then this fails.

You have no try/catch block to pick up the failure, so the exception goes unhandled.
May 6 '10 #5

tlhintoq
Expert 2.5K+
P: 3,525
sweety56 ( mailto:psusmitha@yahoo.com ) has reported this post:


http://bytes.com/topic/net/answers/8...de#post3569354

This is part of this thread:
ArgumentOutOfRangeException was unhandled by user code http://bytes.com/topic/net/answers/8...dled-user-code

This is the reason that the user gave:
problematic



This message has been sent to all moderators of this forum, or all administrators if there are no moderators.

Please respond to this post as applicable.
Please stop repeatedly reporting this thread. If you do it again I'm going to delete the thread completely.

As stated on the report page, reporting a message to the moderators and experts is to report spam and violations of the Bytes guidelines.

It is not there so you can try to elevate your problem for more attention because you are impatient.
May 7 '10 #6

Post your reply

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