Get One Value From DataTable

J

Jordan Richard

I'm just wondering if there is a faster way (better performance or fewer
lines of code) to retrieve one single value from a DataRow.

Here is what I'm currently doing when I just need a single integer value
from the DataRow:

int someID = 0;
if (DT.Rows.Count == 1) {
DataRow DR;
DR = DT.Rows[0];
someID= Convert.ToInt32(DR["SomeID"]);
}


Please note that I'm not using ExecuteScalar to retrieve one single value
from the underlying data source because I'm using many other columns in that
DataRow elsewhere in the same method.

Thanks!
 
J

Jim Hughes

No need to create the DR object if you are not going to use it more than
once.

int someID = 0;
if (DT.Rows.Count == 1) {
someID= Convert.ToInt32(DT.Rows[0]["SomeID"]);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top