0

I would like to count the number of empty cell in Excel sheet.

I read the double values for all Excel sheet but I can't count the null values since the code below does not work probably.

string path_connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + testbox_bath.Text + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";

OleDbConnection connection = new OleDbConnection(path_connection);

OleDbDataAdapter adapter_STDsamverkan = new OleDbDataAdapter("Select [Kvalitetsklass (null)],[Std samverkan (null)]" + "from [Sheet1$]", connection);

DataTable dt_STDsamverkan = new DataTable();

adapter_STDsamverkan.Fill(dt_STDsamverkan);

for (int i = 0; i < dt_STDsamverkan.Rows.Count; i++)
{    
   if (dt_STDsamverkan.Rows[i].Field<double>(1) == null)
   {
        STDsamverkan_count2++;
   }
}

The hint on the IF statement is

the result of the expression is always 'false' since a value of type 'double' is never equal to 'null' of the type 'double?'

What should I do?

Yamur
  • 339
  • 6
  • 20
  • possible duplicate of [Best way to check if a Data Table has a null value in it](http://stackoverflow.com/questions/4604414/best-way-to-check-if-a-data-table-has-a-null-value-in-it) – ryanyuyu Jun 16 '15 at 16:45
  • you almost certainly want to check for DBNull which is different than null; DBNull means there is no data for something; null means that something doesnt exist – Ňɏssa Pøngjǣrdenlarp Jun 16 '15 at 17:01
  • Thanks @ryanyuyu and @Plutonix. I have tried to use `dt_STDsamverkan.Rows[i].Field(1) == DBNull.Value`, but does not work. I got: The result of the expression is always 'false' since a value of type 'double' is never equal to 'null' of type 'double?' – Yamur Jun 16 '15 at 17:06
  • How can I work with the double values read from excel sheet? – Yamur Jun 16 '15 at 18:00

1 Answers1

0

I use Double in reading Datatable. The double is nullable.

The answer for this question under the link below:

double and null in c#

Yamur
  • 339
  • 6
  • 20