C# .NET - I need to get the value of DataTable

Asked By matt cupryk on 27-Sep-06 12:07 PM
string val = TEXTDT[var.name]; // The answer from text file  of

Where TEXTDT is a datatable, with column names var.name

I need to get the value somehow.

TEXTDT[var.name].var.no; I am not sure how to do this/

var.name is the row name

index the row using var.no


Here is the Table called targetTable


QUEST    S_DAT      NAME                  INTRO    PART      => These are the col using var.name
000001   20060101  MATT                    01        1            => These are the rows using var.no
000002   20060101  SHELL                    01        2

Looping Through A DataTable

F Cali replied to matt cupryk on 27-Sep-06 12:38 PM

If you want to loop through a DataTable, here's one way of doing it:

foreach (DataRow dataRow in DataTable1.Rows) {

    foreach (DataColumn dataColumn in DataTable1.Columns) {

        string val = dataRow[dataColumn].ToString();

   }

}

In this code, since the data is in the DataTable already, you can get the column names from it already.

Well I go this problem now:-(

matt cupryk replied to F Cali on 27-Sep-06 02:19 PM
I don't understand why this does not work.


 object cell = _dtProjectQuestion.Rows[0]["ProjectQuestionName"];
 object value = TEXTDT.Rows[0][cell.ToString()];

In the TEXTDT there  is a column table "NAME"

cell = "NAME"

I don't understand.

Check Column Name

F Cali replied to matt cupryk on 27-Sep-06 03:07 PM
Make sure that the TEXTDT data table has a column with a name of "NAME", which should exactly match including the capitalization.  Also make sure that the "NAME" column in the TEXTDT data table doesn't have extra spaces at the end because "NAME" is not the same as "NAME    ".  Looking at your text file, it might have included the extra spaces at the end for the column name.
How Can I check?
matt cupryk replied to F Cali on 27-Sep-06 03:11 PM

This is what I am thinking. I am also thinking it was in lower case.

What can we do? I need your help badly.

Debug Trace
F Cali replied to matt cupryk on 27-Sep-06 03:14 PM

One way is to trace your application and put a break point in this line:

object value = TEXTDT.Rows[0][cell.ToString()];

Then once it stopped, check the columns defined for the TEXTDT data table and check the column name.  I suspect that the column name is different.

What should I check it under.
matt cupryk replied to F Cali on 27-Sep-06 03:35 PM
-  TEXTDT.Rows[0] {System.Data.DataRow} System.Data.DataRow
  HasErrors false bool
-  ItemArray {Dimensions:[13]} object[]
  [0] "0000000001" object {string}
  [1] "20060918" object {string}
  [2] "Beth Anders                                                                     " object {string}
  [3] "01" object {string}
  [4] "1" object {string}
  [5] "  " object {string}
  [6] "69" object {string}
  [7] "1" object {string}
  [8] "1" object {string}
  [9] "04" object {string}
  [10] "2" object {string}
  [11] "CO" object {string}
+  [12] {} object {System.DBNull}
  RowError "" string
  RowState Added System.Data.DataRowState
+  Table {targetTable} System.Data.DataTable
+  Static members  
+  Non-Public members  
Check Columns
F Cali replied to matt cupryk on 27-Sep-06 03:37 PM
You have to check the columns collection of the TEXTDT data table and not the rows.  Look for the TEXTDT data table in the Locals tab (during the debug/trace) and expand it.  Look for the Columns collection and expand that and look for the names used for the columns.
Ron I have the following:
matt cupryk replied to F Cali on 27-Sep-06 03:39 PM

-  _list Count = 13 System.Collections.ArrayList
+  [0] {QUEST} object {System.Data.DataColumn}
+  [1] {S_DAT} object {System.Data.DataColumn}
+  [2] {NAME } object {System.Data.DataColumn}
+  [3] {INTRO} object {System.Data.DataColumn}
+  [4] {PART } object {System.Data.DataColumn}
+  [5] {INT00} object {System.Data.DataColumn}
+  [6] {YEARS} object {System.Data.DataColumn}
+  [7] {AREA } object {System.Data.DataColumn}
+  [8] {LIKE } object {System.Data.DataColumn}
+  [9] {SERV } object {System.Data.DataColumn}
+  [10] {ACTIV} object {System.Data.DataColumn}
+  [11] {INT01} object {System.Data.DataColumn}
+  [12] {INT  } object {System.Data.DataColumn}
+  Raw View  

There looks like a space

how do remove spaces infont?

Response
F Cali replied to matt cupryk on 27-Sep-06 03:43 PM

What you can do is to loop through the Columns collection and trim the trailing spaces.

foreach (DataColumn dataColumn in TEXTDT.Columns) {

    dataColumn.ColumnName = dataColumn.ColumnName.Trim();

}

sathish replied to F Cali on 10-Nov-10 06:13 AM
Hi

GO through the below link

http://sathishmncl.blogspot.com/2010/10/to-get-datacolumn-name-in-datatable-in.html