Get Values from DataTable by row and column name

smr5

I'm typically used to traditional table in SQL where I have multiple columns with rows populated. I execute a stored procedure and store all the data in DataTable and loop through the table to get the results I need. For example,

 public static DataTable getInfo (string sessionID)
    {
        try
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SandBox"].ConnectionString);
            SqlCommand cmd = new SqlCommand("GetSessionInfo", conn);
            cmd.Parameters.AddWithValue("SessionGUID", sessionID);
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            throw;
        }
    }

I would load the DataTable:

 DataTable infoTbl = new DataTable();
 infoTbl = getInfo(lbldatabasesessionID.Text);

And I would use foreach loop to loop through the DataTable.

foreach (DataRow row in infoTbl.Rows)
{
   string x = col.ToString();
}

The issue I run into is the database guy gave me a stored procedure that returns a different output (different from what I'm used to). It's a row based.

enter image description here

The only way I can access for example the First Name is if I hard code the position like:

string firstName = infoTbl.Rows[16][2].ToString();

I don't feel comfortable doing this since the position could potentially change. How would I access ElementValue by knowing the name knowing ElementType and ElementName?

Any suggestions?

Mike Burdick

Using DataSet:

        string firstName = string.Empty;

        DataRow row = table.Select("ElementType = 'Demographics' AND ElementName = 'FirstName'").FirstOrDefault();

        if (row != null)
        {
            firstName = (string)row["ElementValue"];
        }

Using Linq:

        string firstName = table.AsEnumerable()
            .Where(f => f.Field<string>("ElementType") == "Demographics" && 
                f.Field<string>("ElementName") == "FirstName")
            .Select(f => f.Field<string>("ElementValue")).FirstOrDefault();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

datatable get specific column from row

From Dev

Get MAX from row with column name (SQL)

From Dev

WPF: Get column name from selected row

From Dev

How to get name of datatable column?

From Dev

How to get list of one column values from DataTable?

From Dev

Get distinct values from a column of DataTable in .NET 2.0

From Dev

How to get list of one column values from DataTable?

From Dev

How to get values in a column of a DataTable?

From Dev

R: Get column name based on row values in R

From Dev

How to store column name as first row in datatable?

From Dev

Mark Selected Datatable Row based on Column Values

From Dev

How to get a subset DataTable from another DataTable filtered by few column values?

From Dev

How to get a subset DataTable from another DataTable filtered by few column values?

From Dev

Get specific value from table with table name, row and column index

From Dev

Copy DataTable Column's values into another DataTable row C#

From Dev

Get specific column foreach row in DataTable

From Dev

sql query get multiple values from same column for one row

From Dev

SQL query to get computed values from the same column in one row

From Dev

Remove row from datatable if column is not numeric

From Dev

Get values from SQL table along with column name as list in Python

From Dev

Get values from SQL table along with column name as list in Python

From Dev

extract values from DataTable with single row

From Dev

C# - How can i get header row from datatable and arrange it vertically down a column?

From Dev

Pandas check row list values with column name

From Dev

count row values as column name mysql

From Dev

Replacing row values by column name in R

From Dev

Sql mapping with column name and first row values

From Dev

Sql mapping with column name and first row values

From Dev

Get Selected Row From DataTable in Shiny App

Related Related

  1. 1

    datatable get specific column from row

  2. 2

    Get MAX from row with column name (SQL)

  3. 3

    WPF: Get column name from selected row

  4. 4

    How to get name of datatable column?

  5. 5

    How to get list of one column values from DataTable?

  6. 6

    Get distinct values from a column of DataTable in .NET 2.0

  7. 7

    How to get list of one column values from DataTable?

  8. 8

    How to get values in a column of a DataTable?

  9. 9

    R: Get column name based on row values in R

  10. 10

    How to store column name as first row in datatable?

  11. 11

    Mark Selected Datatable Row based on Column Values

  12. 12

    How to get a subset DataTable from another DataTable filtered by few column values?

  13. 13

    How to get a subset DataTable from another DataTable filtered by few column values?

  14. 14

    Get specific value from table with table name, row and column index

  15. 15

    Copy DataTable Column's values into another DataTable row C#

  16. 16

    Get specific column foreach row in DataTable

  17. 17

    sql query get multiple values from same column for one row

  18. 18

    SQL query to get computed values from the same column in one row

  19. 19

    Remove row from datatable if column is not numeric

  20. 20

    Get values from SQL table along with column name as list in Python

  21. 21

    Get values from SQL table along with column name as list in Python

  22. 22

    extract values from DataTable with single row

  23. 23

    C# - How can i get header row from datatable and arrange it vertically down a column?

  24. 24

    Pandas check row list values with column name

  25. 25

    count row values as column name mysql

  26. 26

    Replacing row values by column name in R

  27. 27

    Sql mapping with column name and first row values

  28. 28

    Sql mapping with column name and first row values

  29. 29

    Get Selected Row From DataTable in Shiny App

HotTag

Archive