Click here to Skip to main content
15,883,901 members
Articles / General Programming / Performance
Tip/Trick

Accessing Value from System.Data.DataTable : Tip

Rate me:
Please Sign up or sign in to vote.
3.13/5 (8 votes)
23 Oct 2010CPOL 27.9K   4   8
Accessing Value from System.Data.DataTable
I frequently notice that most programmers access data from a datatable using column indexes as shown below:
public class TestClass
{
  public void MyTestMethod()
  {
    //GetTableData Method fetches data from database using a sql query.
    DataTable dt = DataAccessLayer.GetTableData();
    
     foreach(DataRow dRowin dt.Rows)
     {
        //Accessing data through column index
        int empId = Convert.ToInt32(dRow[0]);
     }
  }
}


In the above example what if the column order in the SQL query fetching data changes, your application will break for sure.

Always access the values through column names as shown below:
public class TestClass
 {
    private const string EMP_ID = "EmpId";
    public void MyTestMethod()
    {
        //GetData fetches data from the database using a SQL query
        DataTable dt = DataAccess.GetTableData();
        foreach (DataRow dRow in dt.Rows)
        {
           //Accessing data through column name
           int empId = Convert.ToInt32(dRow[EMP_ID]);
        }
     }
 }

The code above won't break, even if the column order is changed.

Use a constant variable to hold the column names at a single place so that even if the column name changes in the future then you will have to modify the code in only one place.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have good knowledge on Application Development, Developing components, Participating in design workshops and reviews. Particularly interested in client/server and windows applications. i have working experience with Oracle 10g, 11g, PostgreSQL, and MS-SQL Server Databases.

Comments and Discussions

 
GeneralReason for my vote of 2 But what if you change the column na... Pin
Ivan Ičin2-Nov-10 10:43
Ivan Ičin2-Nov-10 10:43 
GeneralReason for my vote of 1 for dummys Pin
staticmain2-Nov-10 1:24
staticmain2-Nov-10 1:24 
GeneralReason for my vote of 4 Nice tip eswa Pin
Sivaraman Dhamodharan28-Oct-10 23:27
Sivaraman Dhamodharan28-Oct-10 23:27 
GeneralReason for my vote of 4 nice tip. helpful for my project. Pin
Foyzul Karim28-Oct-10 19:17
professionalFoyzul Karim28-Oct-10 19:17 
GeneralReason for my vote of 3 much slower Pin
tsmike26-Oct-10 1:37
tsmike26-Oct-10 1:37 
GeneralReason for my vote of 3 good tips for beginners Pin
Bikash Shrestha From Nepal24-Oct-10 6:35
Bikash Shrestha From Nepal24-Oct-10 6:35 
Generalnice to know. that most of us already know. Pin
Hiren solanki22-Oct-10 22:31
Hiren solanki22-Oct-10 22:31 
GeneralYou might want to modify your code to use the constant, you ... Pin
Ron Beyer22-Oct-10 12:06
professionalRon Beyer22-Oct-10 12:06 
You might want to modify your code to use the constant, you declare it, but you still use a string literal in the loop.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.