6

I'm a low-level algorithm programmer, and databases are not really my thing - so this'll be a n00b question if ever there was one.

I'm running a simple SELECT query through our development team's DAO. The DAO returns a System.Data.DataTable object containing the results of the query. This is all working fine so far.

The problem I have run into now:
I need to pull a value out of one of the fields of the first row in the resulting DataTable - and I have no idea where to even start. Microsoft is so confusing about this! Arrrg!

Any advice would be appreciated. I'm not providing any code samples, because I believe that context is unnecessary here. I'm assuming that all DataTable objects work the same way, no matter how you run your queries - and therefore any additional information would just make this more confusing for everyone.

Giffyguy
  • 20,378
  • 34
  • 97
  • 168

3 Answers3

11

Just the basics....

yourDataTable.Rows[ndx][column]

where ndx is the row number (starting at 0) where column can be a DataColumn object, an index (column n), or the name of the column (a string)

yourDataTable.Rows[0][0]
yourDataTable.Rows[0][ColumObject]
yourDataTable.Rows[0]["ColumnName"]

to test for null, compare to DBNull.Value;

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
1

You mean like table.Rows[0]["MyColumnName"]?

Hans Malherbe
  • 2,988
  • 24
  • 19
0

If you wan't to extract the row with it's ID = 5(ie the Primary key) and get its value for the Column called Description.

DataRow dr = myDataTable.Rows.Find(5);
String s = dr["Description"].ToString();
mikek3332002
  • 3,546
  • 4
  • 37
  • 47