Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

datatable

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?

like image 613
Marco Dinatsoli Avatar asked Feb 13 '23 02:02

Marco Dinatsoli


2 Answers

Use the indexer

dt.Rows[i][j] != null && string.IsNullOrEmpty(dt.Rows[i][j].ToString())
like image 119
Selman Genç Avatar answered Feb 14 '23 15:02

Selman Genç


try this

 if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
like image 34
Mairaj Ahmad Avatar answered Feb 14 '23 16:02

Mairaj Ahmad