0

I have a data adapter with a method that that takes a variable and returns all entries that matches the variable:

e.g.

TableAdaptor ta = new TableAdaptor();
DataTable dt = ta.GetUserByUserID(UserID);

But I can't for the life of me figure out how to return a single cell value from the method. Since it's a strongly typed datatable, the column name becomes a property of the datatable so I can do:

dt.UserIDColumn.ToString();

But this returns the name of the column and not the value of the cell. And if I try:

dt.Rows[0]

there is no column property according to VS10's intellisense. What am I missing here?

Tony
  • 1,839
  • 10
  • 27
  • 48
  • What is the name of the Row that you are trying to retrieve the value(s) of ..? for example you could do string user_ID = row["UserIDColumn"].ToString(); Please confirm if you need to get more than one row of ID's if so then you would need to do a foreach loop and I could post an example of how to do this for you if necessary... – MethodMan Oct 09 '12 at 17:27
  • Try dt.Rows[0]["ColumnName"]. – Josh C. Oct 09 '12 at 17:30
  • Okay that worked... but according to http://www.asp.net/web-forms/tutorials/data-access/introduction/creating-a-data-access-layer-cs isn't that the format for a loosely typed datatable? I thought the whole point of using strongly typed datatables is to have the column names as a property of the rows like `DataTable.Rows[index].columnName` instead of `DataTable.Rows[index]["columnName"]` – Tony Oct 09 '12 at 17:37
  • If I remember correctly, the strongly typed table will itself be indexable, so that you can say `DataTable[index].CustomColumn` and get a strongly-typed value. – Ann L. Oct 09 '12 at 17:44
  • Ah. That explains it. I guess the article I was reading is a little outdated. – Tony Oct 09 '12 at 17:47

1 Answers1

3

OK, I'm assuming you're not really declaring a variable of type DataTable to hold your strongly typed data table, because as the base class, that won't have all the strongly typed columns, etc.

Try this:

StronglyTypedDataTable dt = ta.GetUserByUserID(UserID);   
StronglyTypedRow row = dt[0];
var value = row.ColumnName;

If memory serves, the strongly typed table will be indexable, and you'll be able to access strongly typed data rows. Those will have strongly typed properties representing the individual cells, by name.

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • I suppose I failed to mention that I did add a dataset with the data adapter to make the method GetUserByUserID return a strongly typed data table. So your suggestion `DataTable[index].CustomColumn` did work. I guess the syntax given in the article (`DataTable.Rows[index].columnName`) was outdated so it did not work. – Tony Oct 09 '12 at 17:54