locked
How to check particular column and it's values RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    I have one datatable. it contains 100 columns.

    the problem is i don't want to check all the columns in the table.

    I just want to check column from 25 to 50 in sequence.

    finally i have the score and that i want to compare in that column (25 to 50) values.

    if any score value is matching then we want to return find the  column name.

    How to do this using data row.

    below is code for sample.

                               

    val1 val2 val3 val25 val26 val100
    80 100 20 90 20 90 80 40 30

    int Score = 80;

     foreach (DataRow row in result)

        {
             // Here i want to check row[25] to row[50]

             
       }

    Output : val26 (if it is matching values then return column name)

    (val26 is a columnname)

    Friday, October 13, 2017 10:15 AM

Answers

  • User475983607 posted

    In my table some of the columns has null values.

    when i am executing i getting error

    Error : Object cannot be cast from DBNull to other types.

    for (int colIndex = 5; colIndex <= 35; colIndex++)
    {
    double str1 = Convert.ToDouble(row[colIndex]); // Error : Object cannot be cast from DBNull to other types.
    if (str1 == Score)// compare maxvalue
    {
    ColName = dt.Columns[colIndex].ColumnName;
    break; // if matching the value then get the columnname and then exit the loop
    }
    }

    How to handle null values if the column and rows has null values...

    Simply check for DbNull.

    if (row[colIndex] != DBNull.Value)
    {
        //do something
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 13, 2017 12:38 PM

All replies

  • User475983607 posted

    If I understand you have two questions. 

    1) How to create a loop in C# that counts from 25 to 50.  See the C# for loop reference documentation.

    for(int i=25; i<=50; i++)
    {
        var value = row[i];
    }

    2) How to get the column name using the data row indexer.  See the DataRow reference documentation and the DataColumn.ColumnName property.

    string columnName = row[25].ColumnName

    Friday, October 13, 2017 12:04 PM
  • User-1024101449 posted

    In my table some of the columns has null values.

    when i am executing i getting error

    Error : Object cannot be cast from DBNull to other types.

    for (int colIndex = 5; colIndex <= 35; colIndex++)
    {
    double str1 = Convert.ToDouble(row[colIndex]); // Error : Object cannot be cast from DBNull to other types.
    if (str1 == Score)// compare maxvalue
    {
    ColName = dt.Columns[colIndex].ColumnName;
    break; // if matching the value then get the columnname and then exit the loop
    }
    }

    How to handle null values if the column and rows has null values...

    Friday, October 13, 2017 12:08 PM
  • User475983607 posted

    In my table some of the columns has null values.

    when i am executing i getting error

    Error : Object cannot be cast from DBNull to other types.

    for (int colIndex = 5; colIndex <= 35; colIndex++)
    {
    double str1 = Convert.ToDouble(row[colIndex]); // Error : Object cannot be cast from DBNull to other types.
    if (str1 == Score)// compare maxvalue
    {
    ColName = dt.Columns[colIndex].ColumnName;
    break; // if matching the value then get the columnname and then exit the loop
    }
    }

    How to handle null values if the column and rows has null values...

    Simply check for DbNull.

    if (row[colIndex] != DBNull.Value)
    {
        //do something
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 13, 2017 12:38 PM
  • User-1024101449 posted

    Thanks.. Ir's working...

    Friday, October 13, 2017 12:47 PM