Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from a DataSet

Status
Not open for further replies.

SigmaX2

Technical User
Aug 14, 2005
9
US
Hey;

I'm pretty new to C#, and have succeeded in loading a deliminated text file with a code snippet I found online. It loads the data into a DataSet.

All I've been able to learn from google on getting data from DataSets focuses on putting that data into a DataGrid. I don't want a datagrid. How do I go about reading the data row by row in the code (datagrids aside... which everybody seems to be obsessed with... but my program has a console interface.)
Any assistance is appreciated.
SigmaX
 
Thanx for the reply.

ExecuteReader(); is exactly the method I want to use, and your link is worth bookmarking... but I still can't find anything on how to use it with a dataset. My data is loaded directly from a text file... and there is no SqlCommand object on which to use ExecuteReader();

*sigh*
Been a long day...
SigmaX
 
chMohan's link seems to have the info you're looking for. I won't be answering any more Q's at ~1AM after 12 hours of coding because I just can't read at that point :-/

Hope you got what need.

All hail the INTERWEB!
 
MMkay. I'm making progress, but I'm still having troubles. When I do:
Code:
foreach ( DataTable table in loaded_chart.Tables )
{
  int i = 0;
  foreach ( DataRow row in table.Rows )
  {
    lms_table[i, 0] =  (float) row[keytext];
  }
}
('keytext' being a string with the desired column name, 'loaded_chart' being the dataset, and 'lms_table' being a float array)

I get the following error:
Code:
Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.

Any help? I get the same error if I just do
Code:
Console.WriteLine(row[keytext]);
Thanx again,
SigmaX
 
I think You should say oRow not row

and try

int i = 0;
int l=0;
foreach ( DataRow row in table.Rows )
{
l=table.Rows.Index;
lms_table[l] = (float) row[keytext];
}


 
Okay, I've made some more progress. I have the data entering into the array now. But now the foreach() statement is executing once through, then terminating in the same error I was getting above (I dunno, maybe that was the problem in the first place).

Any clues? I fear I'm totally lost now.
 
In one of the tables keytext is not a column seems to me
 
That's what I figured at first too. But it executes the block successfully once, and throws the exception the second time it comes to the row[keytext] line.

What did you mean earlier when you said use oRow instead of row? Is it of any significance?
Thanx,
SigmaX
 
Okay
use try catch block and use the exception object errormessage and source properties to get more info
 
You know when you loop again and again you have to clear the array each time dont u?Or reinitialize it?
 
No, I guess i didn't know that. Clue me in with a little explanation, because from what I can tell that's my problem.

I can get the data into a Console.Write now (Dunno why I couldn't before), but when I try to put it into my global array it goes haywire.
Thanx,
SigmaX
 
you see when u first populate the array it has elements when it does the loop and next time you try to set it again you have to clear it before setting value again
 
Mmkay... so how do I do that and preserve my data? I'm not sure I really understand what you mean. (Forgive my ignorance).
SigmaX
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top