Click here to Skip to main content
12,954,601 members (55,613 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I am newbie to c#. I working on a project. I am trying to loop thru a dataTable containing about 100,000 rows. I am using index as varibale to access the rows of dataTable like following.
for(int index=0; index< DT.rows.count;index++)
    string member ="";
    member = DT.Rows[index]["memberName"];

After running 32768 it is showing error as There is no row at postion at -32768. I am thinking that this is becuase of DataTable Row index accepts only integer value as index and it's value is more than 32678 it is showing error. Can anybody tell me how to get rid of this???

Thanks in advance.

Posted 13-May-12 17:48pm
Updated 13-May-12 17:51pm
SAKryukov 13-May-12 23:55pm
No, this is not the reason: calculate the Int32 range or just read MSDN help on this type. What is the exception information, in what line of code? show that code...
Shahin Khorshidnia 14-May-12 1:05am
I think you used Int16 instead of int try to review your code.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

hi this is only because range of integer i.e int16 datatype is -32768 to 32767 so after looping 32767 times it jumps into negative side and this cycle repeats.
So to avoid this use long datatype or you can also go with simply int.
i'll suggest you to write (DT.rows.count - 1) as index stars from zero this may also cause bug
accept solution if you have got the solution.

VijayChauhan123 14-May-12 0:42am
yes, If Use Long Int then it will solve the problem...
Shahin Khorshidnia 14-May-12 0:56am
No, the rang of int is -2,147,483,648 to 2,147,483,647 ... Look at here:
preet88 14-May-12 1:21am
@shahin: yeah you are correct but here the problem may be caused due to the usage of int16... because if it has 100000 rows then this is the most probable reason according to me.
Moreover Chowdary i'll suggest you to write (DT.rows.count - 1) as index stars from zero this may also cause bug
chowdary2005 14-May-12 1:57am
but DataTable .rows[index] Where index accepts only int not accepting longint.

Kindly look into it.
preet88 15-May-12 0:09am
@chowdary2005 there is no such data type as longint however long is there. Do run your loop condition upto (DT.rows.count - 1) instead of DT.rows.count
chowdary2005 15-May-12 20:42pm
I tried using the above, but go the same error. I beleive using DT.rows.count -1doesn't make any difference when Datatable loop need to run 1 lakh times and we are using int as index
preet88 16-May-12 0:38am
@chowdary2005 hi i have tried similar kind of code using int and for 100000 rows in datatable. i suppose there is no problem in the code for fetching data please debug into the code and check for the rows count or values in column whether they are present or not...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


foreach(DataRow row in DT.Rows)
     string member = row["memberName"];

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 17 May 2014
Copyright © CodeProject, 1999-2017
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