Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataTable
Hello,
 
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.
 
Chowdary.
Posted 13-May-12 17:48pm
Edited 13-May-12 17:51pm
JF201553.2K
v2
Comments
SAKryukov at 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...
--SA
Shahin Khorshidnia at 14-May-12 1:05am
   
I think you used Int16 instead of int try to review your code.
Rate this: bad
good
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.
 
thanks...
  Permalink  
v3
Comments
VijayChauhan123 at 14-May-12 0:42am
   
yes, If Use Long Int then it will solve the problem...
Shahin Khorshidnia at 14-May-12 0:56am
   
No, the rang of int is -2,147,483,648 to 2,147,483,647 ... Look at here: http://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx
preet88 at 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 at 14-May-12 1:57am
   
but DataTable .rows[index] Where index accepts only int not accepting longint.
 
Kindly look into it.
preet88 at 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 at 15-May-12 20:42pm
   
Hello,
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 at 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
good
Please Sign up or sign in to vote.

Solution 2

Use
 
foreach(DataRow row in DT.Rows)
{
     string member = row["memberName"];
}
  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 494
1 CHill60 405
2 OriginalGriff 345
3 PIEBALDconsult 285
4 BillWoodruff 213


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 17 May 2014
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