4

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?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • When do you want to know if it is `null` or empty? Before you fill the table or afterwards? Also, why, what do you want to do then? Basically your question can be simplified to _"How can i access a field of a DataRow via column-index"_ and that was asked often and is documented pretty well. – Tim Schmelter Sep 09 '14 at 12:35
  • @TimSchmelter I want to do that because I keep getting error `The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.` when inserting the datatable to my sql bulk copy and Iread in this question http://stackoverflow.com/questions/18140012/sqlbulkcopy-the-given-value-of-type-string-from-the-data-source-cannot-be-conv that is because the empty string, but even after applying the answer below, I still get that error , could you help please? – Marco Dinatsoli Sep 09 '14 at 12:37

6 Answers6

7

Use the indexer

dt.Rows[i][j] != null && string.IsNullOrEmpty(dt.Rows[i][j].ToString())
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

try this

 if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

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.

KatariaA
  • 752
  • 1
  • 7
  • 22
0
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
  }
}
cyb tyser
  • 1
  • 1
  • 5
    Why should OP use your code? Please explain your answer. – Zippy Nov 09 '16 at 14:20
  • This does not appear to answer the question. OP is asking about working with a `DataTable` not a `dataGridView1`. – dbc Nov 10 '16 at 09:01
0

//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
}
VinothRaja
  • 1,405
  • 10
  • 21
0

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)

YeinCM-Qva
  • 141
  • 2
  • 15