Reputation: 1566
I have a generic function which extracts a datatable from a query.
I want to loop through the datatable, extracing the values from the datatable. I have two fields, ID and BrandName.
How do I loop through the datatable pulling through the ID numbers and the BrandType names?
Upvotes: 0
Views: 125
Reputation: 3902
You need something very much like this:
DataTable result = new FunctionThatReturnsDataTable();
MyObject myObject = new MyObject();
foreach (DataRow row in result.Rows)
{
myObject.ID = Convert.ToInt32(row["ID"].ToString());
myObject.BrandName = row["BrandName"].ToString();
}
Upvotes: 3
Reputation: 46929
foreach(var row in dt.Rows)
{
var id = (int)row["ID"];
var brandName = (string)row["BrandName"];
}
Upvotes: 3