Click here to Skip to main content
12,549,332 members (47,705 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

Accessing Value from System.Data.DataTable : Tip

, 23 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 2 But what if you change the column na... Pin
ivanicin2-Nov-10 10:43
memberivanicin2-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
sirama200428-Oct-10 23:27
membersirama200428-Oct-10 23:27 
GeneralReason for my vote of 4 nice tip. helpful for my project. Pin
Foyzul Karim28-Oct-10 19:17
memberFoyzul 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
Silic0re0922-Oct-10 12:06
memberSilic0re0922-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 23 Oct 2010
Article Copyright 2010 by TweakBird
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid