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()
int requiredamount = 0;
IsInt = Int32.TryParse(txtreceipt.Text, out RequiredRecords);
if (txtamount.Text != "")
requiredamount = Convert.ToInt32(txtamount.Text);
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;
lbltotrec.Text = dt.Rows.Count.ToString();
rowcount = dt.Rows.Count;
lblmsg.Text = "Sorry!Records or Amount greater than Input Data.";
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);
int count = 0;
number = rndNumber.Next(1, max + 1);
while (count <= Convert.ToInt32(txtreceipt.Text));
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);
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
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.
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)