select or access element in datatable

Hi, experts

After I use dataadapter to get data from DB, and fill it in to datatable like below

          SqlDataAdapter DA_subject = new SqlDataAdapter(str_subject_info, str_conn_sql);
            DataTable DT_subject = new DataTable();
            DA_subject.Fill(DT_subject);

Can I use SQL statement in Datatable? or how can I assess the element of the datatable. for example assign an element in datatable to a variable.

Could you please give me example code to do it. thanks in advance

rmtogetherAsked:
Who is Participating?
 
cyberkiwiCommented:
Sorry, try this. Mixed vb and c# code

    for (int i = 0; i < 5; i++)
            {
                int a = DT_subject.Rows[i]["col3"];
            }
0
 
cyberkiwiCommented:
You can access specific cells in datatable using

var value = DT_subject.Rows[a][i];

Or

DT_subject.Rows[i]("id")   -- to access field by name
0
 
rmtogetherAuthor Commented:
Hi, cyberkiwi

I use your code do the following, the "col3" is integer type. but it shows an error (error: method name expected)

            for (int i = 0; i < 5; i++)
            {
                int a = DT_subject.Rows[i]("col3");

            }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.