473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ArgumentOutOfRangeException was unhandled by user code

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
5 1877
tlhintoq
3,525 Expert 2GB
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
@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
3,525 Expert 2GB
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
3,525 Expert 2GB
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
3,525 Expert 2GB
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

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

Similar topics

4
by: vooose | last post by:
Is there any way to handle exceptions thrown by .NET classses? During runtime and at seemingly random times, exceptions similar to the ones shown at the base of this post are thrown. These errors...
3
by: Catalin Porancea | last post by:
Can anybody tell me what is wrong with this code? I would really appreciate some help here. The data grid has 4 columns (0,1,2,3), and 1 is not visible. Whenever I click Edit, I get this error:...
0
by: Blasting Cap | last post by:
I had some old code set to throw an email to me when a user hit an exception. VS 2003 worked fine with the code, VS 2005 when I run it in debug mode pauses over the code each time, and gives me...
4
by: bg_ie | last post by:
Hi, I have the following Program.cs - namespace TestFrameworkApplication { static class Program { /// <summary> /// The main entry point for the application.
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
This is part of my service contract: <OperationContract(), FaultContract(GetType(String))_ Function AddControlStation(...) As Integer This is part of my the service type in AddControlStation():...
1
by: vranis | last post by:
when i try to consume the webserive i get the error web exception was unhandled by usercode
2
by: csprakash | last post by:
Hi All, I want to run some user when an Application is started, or when a worker process is started. Application_Start, Init methods of HttpApplication in Global.asax.cs are of not use, as they...
5
by: Just_a_fan | last post by:
I can get a dump. I can send it in to M$. I can inspect it but cannot get it in my hands so I can not post it here and get comments and/or answers. Can I get hold of that dump somewhere? Is it...
4
by: asmx126453 | last post by:
This is the error i Get Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index It points to this code if (!IsPostBack) ...
1
by: mamtaid | last post by:
in the line asq1 = asq.ExecuteReader(); it is giving the error ExecuteReader: Connection property has not been initialized. can anyone pls help me... using System; using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.