Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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.
C#
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
Updated 13-May-12 17:51pm
v2
Comments
Sergey Alexandrovich Kryukov 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 14-May-12 1:05am    
I think you used Int16 instead of int try to review your code.

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...
 
Share this answer
 
v3
Comments
Rockstar_ 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: http://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx
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
Charles Shob 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
Use

foreach(DataRow row in DT.Rows)
{
     string member = row["memberName"];
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900