ArgumentOutOfRangeException was unhandled by user code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweety56
    New Member
    • May 2010
    • 2

    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

    Code:
     using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    public partial class ResHomePage : System.Web.UI.Page
    {
        SqlConnection cn;
        SqlCommand cmd, cmd2;
        SqlDataReader dr;
        string d, sd;    
        Rest_RegisterClass ss;
        int a, b, m, sum, f;
        DataView dv = new DataView();
        DataRow rec;
        DataTable dt = new DataTable();
        DataColumn col;
        CheckBox c;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            NewConnection();
            sd = Session["resname"].ToString();
            Label2.Text = sd;
            d = Request["a"];
            Label4.Text = DateTime.Now.ToLongTimeString();
            
            Label24.Text ="," + DateTime.Now.ToLongDateString();
            //Label4.Text = Format(Now, "dddd, dd-MMM-yyyy<br>hh:mmtt");
            //Label24.Text = Format(Now, "dddd");
            ss = new Rest_RegisterClass();
            ss.RestId = d;
            lad.Text = ss.RAddress1 + ", " + ss.RAddress2 ;
            lc.Text = ss.RCity;
            ls.Text = ss.RState;
            lz.Text = ss.RZipCode;
            lph.Text = ss.RPhone;
            Lot.Text = ss.ROTime;
            lct.Text = ss.RCTime;
    
    
            MenuClass obj = new MenuClass();
            obj.RestId = d;
            obj.Retrivedealb();
            obj.retrivedeall();
            obj.retrivedeald();
    
            Label24.Text = obj.DayOfW;
    
            Label27.Text = obj.StartTimeB;
            Label28.Text = obj.EndTimeB;
            Label29.Text = obj.StartTimeL;
            Label30.Text = obj.EndTimeL;
            Label31.Text = obj.StartTimeD;
            Label32.Text = obj.EndTimeD;
            cmd = new SqlCommand("select * from restaurant where restaurantid='" + d + "'", cn);
            dr = cmd.ExecuteReader();
            dr.Read();
            img2.ImageUrl = dr["headerimage"].ToString();
            img1.ImageUrl = dr["logo"].ToString();
            dr.Close();
            if (IsPostBack)
            {
                col = new DataColumn("itemno");
                dt.Columns.Add(col);
                col = new DataColumn("itemname");
                dt.Columns.Add(col);
                col = new DataColumn("qty");
                dt.Columns.Add(col);
                col = new DataColumn("itemprice");
                dt.Columns.Add(col);
    
                Cache["Tapas"] = dt;
            }
            Session["total"] = Label19.Text;
        }
        public void NewConnection()
        {
            cn = new SqlConnection(clsConnectionString.StrCon);
            cn.Open();
        }
        protected void LinkButton8_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }
        protected void LinkButton9_Click(object sender, EventArgs e)
        {
            Response.Redirect("SpMenu.aspx?a=" + Session["id11"]);
        }
        protected void mil1_Click(object sender, EventArgs e)
        {
            f = 0;
            Label16.Visible = false;
            Label17.Visible = false;
            listb();
        }
        private ArrayList Timings(string mid)
        {
    
            NewConnection();
            string s = "select StartTime,EndTime from menuavailability where description='" + mid + "' and DayofWeek='" + DateTime.Today.DayOfWeek.ToString() + "'";
            cmd = new SqlCommand(s, cn);
            dr = cmd.ExecuteReader();
            ArrayList a = new ArrayList();
            while (dr.Read())
            {
                a.Add(dr[0]);
                a.Add(dr[1]);
            }
            dr.Close();
            return a;
        }
    Code:
     private void listb()
        {
            Label36.Visible = true;
            string str = "breakfast";
            ArrayList ar = Timings(str);
            DateTime t, t1, t2;
            t = DateTime.Now;
            t1 = (DateTime)(ar[0])/I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
    [/B][/I]
            t2 = (DateTime)(ar[1]);
            double d = (t.Hour * 60) + t.Minute;
            double stime = (t1.Hour * 60) + t1.Minute;
            double etime = (t2.Hour * 60) + t2.Minute;
            if (d < etime)
            {
                Label36.Text = "";
                Label14.Visible = true;
                MIB.Visible = true;
                databreak();
            }
            else
            {
                Label36.Text = "Breakfast time over so not available";
                MIB.Visible = false;
                Label14.Visible = false;
            }
        }
      
        private void listl()
        {
            Label36.Visible = true;
            string str = "lunch";
            ArrayList ar = Timings(str);
            DateTime t, t1, t2;
            t = DateTime.Now;
    
            t1 = (DateTime)(ar[0]);[I][B]//I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
    [/B][/I]
            t2 = (DateTime)(ar[1]);
    
            double d = (t.Hour * 60) + t.Minute;
            double stime = (t1.Hour * 60) + t1.Minute;
            double etime = (t2.Hour * 60) + t2.Minute;
            if (d < etime)
            {
                Label36.Text = "";
                MIB.Visible = true;
                Label6.Visible = true;
    
                datalunch();
            }
            else
            {
                Label36.Text = "Lunch time over so not available";
                MIB.Visible = false;
                Label6.Visible = false;
            }
        }
    
        private void listd()
        {
            Label36.Visible = true;
            string str = "dinner";
            ArrayList ar = Timings(str);
            DateTime t, t1, t2;
            t = DateTime.Now;
            t1 = (DateTime)(ar[0]);//I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
    [/B][/I]        t2 = (DateTime)(ar[1]);
            double d = (t.Hour * 60) + t.Minute;
            double stime = (t1.Hour * 60) + t1.Minute;
            double etime = (t2.Hour * 60) + t2.Minute;
            if (d < etime)
            {
                MIB.Visible = true;
                Label36.Text = "";
                Label7.Visible = true;
                datadinner();
            }
            else
            {
                Label36.Text = "Dinner not available";
                MIB.Visible = false;
                Label7.Visible = false;
            }
        }
        
        private void  fun1()
        {
            dt =(DataTable)(Cache["Tapas"]);
            
            string item1,item2;
            int flag= 0;
           
                foreach(DataGridItem i in MIB.Items)
                {
                    c =(CheckBox )(i.FindControl("chk"));
                    if (c.Checked == true)
                    {
                        if(dt.Rows.Count == 0)
                        {
                            rec = dt.NewRow();
                            rec[0] = i.Cells[1].Text;
                            rec[1] = i.Cells[2].Text;
                            a = (int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text);
                            rec[2] = a ;//qty
                            b = (int.Parse)(i.Cells[4].Text);  //Item price
                            dt.Rows.Add(rec);
                            if (a >= 1) 
                            {
                                rec[3] = b * a;      //Total price
                                m = a * b;
                            }
                            else
                            {
                                rec[3] = b * 1;
                                m = 1 * b;
                            }
                        } 
                        else
                            {
                               foreach(DataRow drw in dt.Rows)
                               {
                                item1 = drw["itemno"].ToString();
                                item2 = i.Cells[1].Text;
                                if(item1 == item2)
                                {
                                    drw["qty"] = ((int.Parse)(drw["qty"].ToString()))+ ((int.Parse)(((TextBox)(i.Cells[5].Controls[1] )).Text));
                                    b =((int.Parse) (i.Cells[4].Text));  //item price
                                    a = ((int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text));
                                    drw["itemprice"] = ((int.Parse)(drw["itemprice"].ToString())) + (b * a);
                                    flag = 1;
                                }
                               }
                               if (flag == 0) 
                               {
                                rec = dt.NewRow();
                                rec[0] = i.Cells[1].Text;    //item no
                                rec[1] = i.Cells[2].Text;    //item name
                                b =((int.Parse)( i.Cells[4].Text));  //item price
                                a = ((int.Parse)(((TextBox)(i.Cells[5].Controls[1])).Text));
                                rec[2] = a;
                                //b=qty
                                rec[3] = a * b;
                                m = a * b;
                                dt.Rows.Add(rec);
                           }
                        }
                    }
                }
                    foreach(DataRow drw in dt.Rows)
                    {
                    sum = sum + ((int.Parse)(drw["itemprice"].ToString()));
                    Label19.Text = sum.ToString();
                    }
                Label7.Text = "";
                DataGrid1.Visible = true;
                DataGrid1.DataSource = dt;
                Cache["Tapas"] = dt;
                DataGrid1.DataBind();
                Cache["ItemAdded"] = dt;
            
            
        }
    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.
    Last edited by tlhintoq; May 6 '10, 07:16 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • sweety56
      New Member
      • May 2010
      • 2

      #3
      Originally posted by tlhintoq
      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.
      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.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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?

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Code:
           t = DateTime.Now;
                  t1 = (DateTime)(ar[0])/I get a error here saying that the ArgumentOUtofRange exception unhandled by the usercode
          [/b][/i]
                  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.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            sweety56 ( mailto:psusmith a@yahoo.com ) has reported this post:




            This is part of this thread:
            ArgumentOutOfRa ngeException 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.

            Comment

            Working...