1

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.

5
  • 1
    why are you not checking for row["SAP_ID"] != null? Commented Aug 23, 2022 at 8:11
  • @Daniel: like only null ?? can u show for one sample Commented Aug 23, 2022 at 8:15
  • Your if condition performs to check is there any column is not null, then proceed. I don't it matches with your requirement. So probably these are 2 approaches: 1. Replace != with ==. 2. Use AND operator && instead of OR operator ||. Commented Aug 23, 2022 at 8:19
  • @YongShun : when I debugged the code, I found for row["FINAL_SR_DATE"] its coming as {} whenever there is blank column. so with what condition I should check. Please provide a sample code Commented Aug 23, 2022 at 8:22
  • How is this related to Excel in the first place? Excel isn't a database. If you use the Access engine to read it you can filter nulls in the SQL statement. If you use ExcelDataReader and eg AsDataSet you can specify which rows to include/exclude based on their values before you create the DataTable Commented Aug 23, 2022 at 8:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

dear Lajos, how can I check in my case ?
@Nad in your if replace the || operator to &&
@Nad every single || should become a &&.
@Nad currently you check whether at least one element differs from null. This will fail if you have some null values, but a field is not null, as you want to ensure that all the fields are differing from null.
Thanks Lajos, simply changing to && from || worked. +1 for nice explanation
1
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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.