AndreaNobili
AndreaNobili

Reputation: 42957

How to access to the content of a DataRow?

I have the following problem.

I have a DataTAble object that represent a single column table, the column is named VulnerabilityReferenceId, something like this:

VulnerabilityReferenceId
167554
167555
167556
167557
167558
167559
167560
167561

So I want create a foreach that access to these row and put the value into a variable

I have done:

foreach (DataRow row in _dt.Rows)
{
    Debug.WriteLine("VulnerabilityReferenceId: "  );
}

But what can I do to access to the value of the current row and put it into an int variable?

Upvotes: 2

Views: 458

Answers (4)

Murali Murugesan
Murali Murugesan

Reputation: 22619

You can use column name as an indexer to get the value as object

foreach (DataRow row in _dt.Rows)
{
    int vulRefId=Convert.ToInt32(row["VulnerabilityReferenceId"]);
    Debug.WriteLine("VulnerabilityReferenceId: " +vulRefId  );
}

Upvotes: 1

Steve
Steve

Reputation: 216293

This could be an approach that read the field and convert it to the required datatype. It requires the reference to DataSetExtension assembly from NET3.5 where you could start to find the DataRowExtensions class

foreach (DataRow row in _dt.Rows)
{
    int id = row.Field<int>("VulnerabilityReferenceId");
    .....
}

Note: I assume that the field VulnerabilityReferenceId is of type integer

Upvotes: 4

dave823
dave823

Reputation: 1211

foreach (DataRow row in _dt.Rows)
{
    String stringVal = row["VulnerabilityReferenceId"].ToString();
    int myId = Convert.ToInt32(stringVal);
}

Upvotes: -1

user3240361
user3240361

Reputation: 417

Try the below:

for(int i=0;i<_dt.Rows.Count;i++)
 {
   Debug.WriteLine("VulnerabilityReferenceId: "+dt.Rows[i][0].ToString());
 }

Upvotes: 0

Related Questions