Connecting Tech Pros Worldwide Forums | Help | Site Map

Retrieving records from datatable

Newbie
 
Join Date: Oct 2008
Posts: 2
#1: Oct 19 '08
Hi,

Can anyone tell me how to retrieve record values from a datatable?.My requirement is like:

Within a for loop I am executing different stored procedures and storing records in a datatable so that i cannot specify paticular column names.i want to write the first row values of each result into a log file.(application is windows service)
Code is :

Expand|Select|Wrap|Line Numbers
  1. DataTable dtTransID = new DataTable(); 
  2. DBConnect(); 
  3. string sp=" exec "+storedProcedure+"  ";
  4. dtTransID =mDB.RunSp(sp);
  5. if(dtTransID.Rows.Count >0)
  6. {
  7. for(i=0;i<dtTransID.Rows.Count;i++)
  8. {
  9. if (storedProcedure!=null)
  10. {
  11. message = message + Datetime.Now+ "\n  + dtTransID.Rows[0] +, -  ";
  12. message = message + dtTransID.Rows[0] + ",";
  13. Globals.log.logMessage(message);
  14.  
  15. }
dtTransID.Rows[0] -This line is returning 'system.data.rows' not any records.
Please help me..
Thanks in advance

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Oct 19 '08

re: Retrieving records from datatable


Perhaps no rows are being returned from your stored procedure? You may also need a datareader.
Newbie
 
Join Date: Oct 2008
Posts: 2
#3: Oct 23 '08

re: Retrieving records from datatable


Quote:

Originally Posted by kenobewan

Perhaps no rows are being returned from your stored procedure? You may also need a datareader.


No kenobewan...That count shows as 8.but its not reading the values.....
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Oct 23 '08

re: Retrieving records from datatable


dtTransID.Rows[0] is a DataRow object, you are trying to use it as a string
if you only want the first column of the first row, try maybe:
dtTransID.Rows[0][0].ToString()
Reply