473,396 Members | 2,087 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,396 software developers and data experts.

How to rectify Exception 'Index Out of Range'

Hey guys,


I am making an application that generates 'ID'
automatically. Let me post part of my code for ya all.



Expand|Select|Wrap|Line Numbers
  1. dt = dB1DataSet1.Tables["company"];
  2.             len = dt.Rows.Count + 1;
  3.             dr = dt.Rows[len];
  4.             code = dr["C_ID"].ToString();
  5.             cde = code.Substring(1, 2);
  6.             ctr = Convert.ToInt32(cde);
  7.  
  8.                 if ((ctr >= 1) && (ctr < 9))
  9.                 {
  10.  
  11.                     ctr = ctr + 1;
  12.  
  13.                     textBox1.Text = "C00" + ctr;
  14.  
  15.  
  16.  
  17.                 }
  18.                 else if ((ctr >= 9) && (ctr < 99))
  19.                 {
  20.                     ctr = ctr + 1;
  21.                     textBox1.Text = "C0" + ctr;
  22.                 }
  23.                 else if (ctr >= 99)
  24.                 {
  25.                     ctr = ctr + 1;
  26.                     textBox1.Text = "C" + ctr;
  27.                 }


here company is table where i have C_ID column.
Every time i try to run this code it gives me error
in line : dr = dt.Rows[len] Error : "Make sure that maximum index on the list is less than the list size".
And also the 'ID' that is getting generated is not being displayed in text box. So do have any suggestions about how do i do it?


Thanks
ajinkya
Mar 1 '11 #1
1 4434
GaryTexmo
1,501 Expert 1GB
For the first issue, the error tells you exactly what you need to know. You're trying to access an index in the array that doesn't exist. Here are the offending lines...

Expand|Select|Wrap|Line Numbers
  1. len = dt.Rows.Count + 1;
  2. dr = dt.Rows[len];
Arrays in C# are 0-indexed, which means the first element in every array is at index 0. You've requested the count for your array, then you even add one to it. The best way to envision this is to just make up numbers and then run through the code.

Lets say your array had a count of 10. This would mean you would have indexes 0 through 9 available and containing data. Your code calculates len as count + 1 = 10 + 1 = 11. You're trying to access index 11 when it doesn't exist. Your code should actually be...

Expand|Select|Wrap|Line Numbers
  1. len = dt.Rows.Count - 1;
  2. dr = dt.Rows[len];
For the second issue, why the value doesn't show up in the textbox, it looks like ctr governs this. You've got an if-then-else-if statements with no else clause, so it's possible to execute through your code and not have the textbox get set. The most likely candidate here is that ctr is evaluating to zero, but perhaps it's another number outside your range. Put a breakpoint on that line and step through to see what it's getting.
Mar 1 '11 #2

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

Similar topics

9
by: Robert Brown | last post by:
If I use _reverse_ wildcard search will it always result in a table scan? Is it possible to get the DB (Oracle or SQL server) to use indexes when doing reverse wildcard match? let's say I have:...
1
by: Thomas | last post by:
Hi dudes, I have a range object for text (not the one in the IE, the one for Mozilla). Now I have a function to reduce/move the range in the text to the LEFT, e.g.:...
4
by: William McIlroy | last post by:
Array Bounds Exception inside system.xml.dll. Test data is a dozen GB (available for the asking on CD). Source code follows. Call into system.xml.dll happens at the while statement.. using...
16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
12
by: Coen | last post by:
Hi, I'm dealing with a master-detail relationship. A datagrid bound to the detail data. When navigating through the records (next record: Me.BindingContext(mDv1).Position += 1), I notice a delay...
8
by: sam | last post by:
hey everybody, this is my first time posting here. i'm pretty new to python and programming in general (as you'll soon work out for yourselves...) i'm trying to code a version of a selection...
0
by: muzu1232004 | last post by:
If I have a table t1 with a column a as follows A 1 1 1 1 1 1 1
0
by: ferlarroca | last post by:
Hi, I am trying to copy a range below another one...... I have defined two ranges as follow: Dim r1 As Range Dim r2 As Range Set r1 = Sheets("Old Index").Range("A9:A200, H9:H200, K9:K200")...
1
by: Steffen Stellwag | last post by:
Truely is often better to scan a table in full passing by an index , but if you can force the optimizer to use an index via a hint for testing and comparing the results. But the index in the...
1
by: evilbungle | last post by:
Hi, I know this is for Access but I was hoping someone may know about Excel and can help me. I have created a Function in VBA for looking up a value in a grid it works fine but only if I place...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.