James Andrew Smith
James Andrew Smith

Reputation: 1566

getting field values from datatables

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

Answers (2)

daniloquio
daniloquio

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

Magnus
Magnus

Reputation: 46929

foreach(var row in dt.Rows)
{
  var id = (int)row["ID"];
  var brandName = (string)row["BrandName"];
}

Upvotes: 3

Related Questions