Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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

 
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:-
 
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 16-Dec-12 17:11pm
Comments
pankajgarg1986 at 16-Dec-12 23:20pm
   
in debugging its show error in line
CSV += lstNumbers[i].ToString() + ",";
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 OriginalGriff 339
2 ChintanShukla 305
3 Richard Deeming 235
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100