Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Pls Help me.When i search gretaer than 255 its gives the followings error:-
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


C#
private void BindGrid()
    {
        try
        {
       
            bool IsInt;
            int RequiredRecords;
            int requiredamount = 0;
           // string CSVData, query, qry;
            IsInt = Int32.TryParse(txtreceipt.Text, out RequiredRecords);
            if (txtamount.Text != "")
            {
                requiredamount = Convert.ToInt32(txtamount.Text);
            }
            if (IsInt)
            {
                if (TotalRecords >= RequiredRecords || TotalAmount >= requiredamount)
                {
                    CSVData = GetRandomNumbersCSV(TotalRecords, RequiredRecords);
                    query = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY name) AS receiptnoid * FROM accountinfo) TempTable WHERE receiptnoid IN(" + CSVData + ")";
                    qry = "select amount from accountinfo where receiptnoid in(" + CSVData + ")";
                    DataTable dt1 = GetRecords(qry);
                    int amount = 0;
                    foreach (DataRow dr in dt1.Rows)
                    {
                        amount += Convert.ToInt32(dr["amount"]);
                    }

                   
                        query = "select * from accountinfo where receiptnoid in(" + CSVData + ")";
                        lbltotamount.Text = amount.ToString();
                        dt = GetRecords(query);
                        Grid.DataSource = dt;
                        Grid.DataBind();
                        lbltotrec.Text = dt.Rows.Count.ToString();
                        rowcount = dt.Rows.Count;
                        
                }
                else
                {
                    lblmsg.Text = "Sorry!Records or Amount greater than Input Data.";
                }
            }
            else
            {
                lblmsg.Text = "Sorry!Pls Check Your Input Data";
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "Error! " + ex.Message;
        }
    }

The Calling Function is:-

C#
public ArrayList RandomNumbers(int max)
    {
            ArrayList lstNumbers = new ArrayList();
            Random rndNumber = new Random();
            int number = rndNumber.Next(1, max + 1);
            lstNumbers.Add(number);
            int count = 0;
            do
            {
                number = rndNumber.Next(1, max + 1);
                if (!lstNumbers.Contains(number))
                {
                    lstNumbers.Add(number);
                }
                count++;
            }
            //while (count <= 10 * max);
            while (count <= Convert.ToInt32(txtreceipt.Text));
            return lstNumbers;
       
    }

    public string GetRandomNumbersCSV(int max, int req)
    {
       
            string CSV = "";
            ArrayList lstNumbers = RandomNumbers(max);
            for (int i = 0; i < req; i++)
                CSV += lstNumbers[i].ToString() + ",";
            CSV = CSV.Remove(CSV.Length - 1);
            return CSV;
       
    }
Posted
Comments
pankajgarg1986 16-Dec-12 23:20pm    
in debugging its show error in line
CSV += lstNumbers[i].ToString() + ",";

With a quick look, it looks like you populate the ArrayList with items based on the value of the parameter max but you query the ArrayList based on the parameter req.

Now if req is greater than max you end up in a situation where you try to get a non-existing item from the list.

Place a breakpoint on the first line of GetRandomNumbersCSV and investigate the values of the parameters to see if this is the case.

Also if the intention is to populate the list for certain amount of items and then list all of them in the lstNumbers (probably a list box), then consider using only one parameter defining the amount of items to both populate and query.
 
Share this answer
 
You have the answer in your question. You must check that lstNumber array length and the number req are of same length.

In your case iterator value i goes beyond the lstNumber array length and causing the index out of range exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900