Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

c# check the empty or null value in all datatable values

I have a DataTable and I initialize it like this:

dataTable = new DataTable();
dataTable.Clear();
for (int i = 0; i < labels.Length; i++)
{
    dataTable.Columns.Add(MyCSVReaderFinal.labels[i]);
}
return dataTable;

where label is array of string like this:

private static string[] labels = { "FARM ID", "FARM OWNER ARABIC", "FARM NUMBER",
"FARM MOBILE", "EMAR NAME ARABIC", "EMARA ID", "AREA NAME ARABIC", "AREA ID",
"REGION NAME ARABIC", "REGION ID", "RECEIVING CENTER NAME ARABIC",
"RECEIVING CENTER ID", "KHALAS", "FARDH", "OTHER LULU", "KHENAIZI", "BOUMAAN",
"BARHI", "JESH KHARMA", "REZIZ", "JABRI", "ANBARET AL-MADINA", "SHISHI",
"DABBAS", "NABTET SAIF", "KHEDRAWI", "HILALI", "MAKTOUMY", "NAMISHI",
"SULTANAH", "BAQLAT AL-TAWAA", "BAQLAT AL-DAHLA", "BAQLAT AL-RARENJA", 
"SUKARY", "SAQEI", "ABU ZEBED", "MAJDOUL", "SHABIBI", "YOUWANI", "YARDI",
"KHADI", "HATIMI", "NEGHAL", "OTHER SAYER", "TOTAL FRUCTIFEROUS",
"TOTAL UN FRUCTIFEROUS", "TOTAL AFHAL", "GENERAL TOTAL", "SENIOR SUPERVISORS",
"ASSISTANT", "DATA ENTRY", "FARM ONWER OR BEHALF" };

I want to check the null of empty string of every value into that data table:

I tried this:

for (int i = 0; i < dt.Rows.Count; i++) {
    for (int j = 0; j < dt.Columns.Count; j++) { 
        if(string.IsNullOrEmpty(dt.Rows[i].)){}
    }
}

but as you see, I tried this: dt.Rows[i]. but I didn't know how to get the value

Could you help me please?

Upvotes: 4

Views: 52526

Answers (6)

YeinCM-Qva
YeinCM-Qva

Reputation: 131

For newer versions of C# there are pretty good things you can do with the null-coalescing operator ?? so in this case you can do:

if (string.IsNullOrEmpty(((dt?.Rows?[0][0]) ?? string.Empty).ToString()))

or

if (string.IsNullOrEmpty((string)((dt?.Rows?[0][0]) ?? string.Empty)))

You can fin more info in several articles like this one:

?? and ??= operators (C# reference)

Upvotes: 0

RIK SENGUPTA
RIK SENGUPTA

Reputation: 1

//Try this code for DataTable When not capture any data from data base

DataTable dt = (arrg);
try { 

     statement 1...................
     statement 2...................
    ..........................
 }
catch (Exception ex)
{
    Alert Message of error
}

Upvotes: 0

cyb tyser
cyb tyser

Reputation: 1

for (int numberOfCells = 0; numberOfCells < this.dataGridView1.Columns.Count; numberOfCells++)
{
  if (String.IsNullOrEmpty(this.dataGridView1.Rows[0].Cells[numberOfCells].Value as String))
  {
     //empty
  }
  else
  {
     //not empty
  }
}

Upvotes: 0

KatariaA
KatariaA

Reputation: 822

Following after @Selman22's answer: You do not need to check for null again in string.IsNullOrEmpty(string) just for the sake of the .ToString() operation. Instead use:

if(dt.Rows[i][j] != null && dt.Rows[i][j] != string.Empty)

You should minimise the check conditions(both .Net provided and explicit). In your case, especially, because you will be checking for each cell of a DataTable which may be expensive with too many condition checks.

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

try this

 if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Use the indexer

dt.Rows[i][j] != null && string.IsNullOrEmpty(dt.Rows[i][j].ToString())

Upvotes: 7

Related Questions