skip to Main Content

I have dt in which there are 4 rows and 35 columns. So I want to check for each row whether there is null value supplied or not. If null is supplied then break the condition if not then go ahead. So I tried the code below, but it’s not working and is accepting the null values as well.

foreach (DataRow row in dt.Rows)
{
    if (row["SAP_ID"] != DBNull.Value || row["CITY"] != DBNull.Value || row["FINAL_SR_DATE"] != DBNull.Value || row["FINAL_SO_DATE"] != DBNull.Value || row["INVOICE_DATE"] != DBNull.Value || row["IP_ID"] != DBNull.Value || row["APPLICABLE_MSA"] != DBNull.Value || row["SITE_CATEGORY"] != DBNull.Value || row["ID_OD"] != DBNull.Value || row["RFI_DATE"] != DBNull.Value || row["BILL_START_DATE"] != DBNull.Value || row["BILL_END_DATE"] != DBNull.Value || row["NO_OF_OPCO"] != DBNull.Value || row["ACTUAL_RENT_AMT"] != DBNull.Value || row["TENURE"] != DBNull.Value || row["GSM_ANTENNA_EXC_SAIL"] != DBNull.Value || row["GSM_ANTENNA_NOTEXC_SAIL"] != DBNull.Value || row["REV_TOT_CNT_GSM_ANTENNA"] != DBNull.Value || row["MW_ANTENNA_OF_UPTO06_DIA"] != DBNull.Value || row["MW_ANTENNA_OF_12DIA"] != DBNull.Value || row["MW_ANTENNA_OF_GREATER12_DIA"] != DBNull.Value || row["HEIGHT_OF_HEIGHEST_ANTENNA"] != DBNull.Value || row["WEIGHT_OF_TOWER_TOP_BTS"] != DBNull.Value || row["WIND_SPEED"] != DBNull.Value || row["POWER_RATING_OF_BTS"] != DBNull.Value || row["FLOOR_SPACE_INDOOR"] != DBNull.Value || row["FLOOR_SPACE_OUTDOOR"] != DBNull.Value || row["EB_STATUS_VALUE"] != DBNull.Value || row["NO_OF_US"] != DBNull.Value || row["HIGHER_RENT"] != DBNull.Value || row["RRH_COUNT"] != DBNull.Value || row["VOLUME_DISCOUNT"] != DBNull.Value || row["VENDOR_NAME"] != DBNull.Value || row["CIRCLE"] != DBNull.Value || row["APPLICABLE_SITE_RENT"] != DBNull.Value)
    {
        ... // do something here
    }
    else
    {
        ... // do something here
    }
}

Please suggest what is wrong here.

2

Answers


  1. Long story short

    || -> &&

    Explanation

    You are asking whether

    x differs from null or y differs from null or …

    When does this evaluate to true? This is true if and only if AT LEAST one of the operands differ from null. As a result, you will either have to check whether

    x differs from null and y differs from null and …

    or, alternatively:

    not (x is null or y is null or …)

    The underlying logic is either to check whether everything differs from null or it’s false that any of them is null.

    Login or Signup to reply.
  2. if (row["SAP_ID"] != DBNull.Value || row["CITY"] != DBNull.Value /* || Following columns */)
    

    The above if statement is checking whether there is any column(s) contain value, which contradicts your requirement:

    If null is supplied then break the condition if not then go ahead


    Approach 1: Check all columns contain value

    foreach (DataRow row in dt.Rows)
    {
        if (row["SAP_ID"] != DBNull.Value && row["CITY"] != DBNull.Value /* && Following columns */)
        {
            // All columns are with value
        }
    
        // TO-DO
    }
    

    Or

    Approach 2: Check any column(s) without value

    foreach (DataRow row in dt.Rows)
    {
        if (row["SAP_ID"] == DBNull.Value || row["CITY"] == DBNull.Value /* || Following columns */)
        {
            // There are column(s) without value
        }
    
        // TO-DO
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search