Click here to Skip to main content
14,978,898 members
Articles / General Programming / Performance
Tip/Trick
Posted 22 Oct 2010

Stats

24K views
4 bookmarked

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
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)

Share

About the Author

TweakBird
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
MemberIvan Ičin2-Nov-10 10:43 
GeneralReason for my vote of 1 for dummys Pin
staticmain2-Nov-10 1:24
Memberstaticmain2-Nov-10 1:24 
GeneralReason for my vote of 4 Nice tip eswa Pin
Sivaraman Dhamodharan28-Oct-10 23:27
MemberSivaraman 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
Membertsmike26-Oct-10 1:37 
GeneralReason for my vote of 3 good tips for beginners Pin
Bikash Shrestha From Nepal24-Oct-10 6:35
MemberBikash Shrestha From Nepal24-Oct-10 6:35 
Generalnice to know. that most of us already know. Pin
Hiren solanki22-Oct-10 22:31
MemberHiren 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 

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.