locked
How can i check column is null? RRS feed

  • Question

  • User-1909885141 posted

    in asp.net, i select some data by using below query.

    And There is code that get user_nm from datatable.

    DataTable test = "select user_id from users";

    test.rows[0]["user_nm"]

    so below code made error.

    test.rows[0]["user_nm"]

    How can i check column is null?


    Tuesday, July 31, 2012 3:52 AM

Answers

  • User-1629691846 posted

    You can use DBNull.value.Equals()

    if (! DBNull.Value.Equals(test.row[0]["user_nm"])) 
       {
          //not null
       }
       else
       {
          //null
       }
    

    OR 

    you can get the value in object first as :

    object value = test.row[0]["user_nm"];
        if (value == DBNull.Value)
    {
    }
    else
    {
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 31, 2012 4:20 AM

All replies

  • User-1629691846 posted

    You can use DBNull.value.Equals()

    if (! DBNull.Value.Equals(test.row[0]["user_nm"])) 
       {
          //not null
       }
       else
       {
          //null
       }
    

    OR 

    you can get the value in object first as :

    object value = test.row[0]["user_nm"];
        if (value == DBNull.Value)
    {
    }
    else
    {
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 31, 2012 4:20 AM
  • User607059406 posted

    yes he is right you can check and use the non null value

    Tuesday, July 31, 2012 5:53 AM
  • User3866881 posted

    How can i check column is null?

    Hi,

    Just try this——

    if(test.Rows[0]["ColumnName"] ==null || test.Rows[0]["ColumnName"] == DbNull.Value)
    {
    } 
    Wednesday, August 1, 2012 2:31 AM
  • User-1758105784 posted
                        if (!string.IsNullOrEmpty(test.Rows[0]["REQUIRED_QTY"].ToString()))
                         {
                             //Do Something.........
                          }
    

    Hope This Will Help

    Wednesday, August 1, 2012 2:38 AM