Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel read cell is empty?

Hey guys my Problem is when i read the cell and in the cell is nothing i have an error how can i check that?

var aExcel = sh.Cells[26, 2].Value.ToString();
 double test = Convert.ToDouble(aExcel);

and a other question how can i read more cells ? because my code is so long …

var aExcel = sh.Cells[26, 2].Value.ToString();
var bExcel = sh.Cells[27, 2].Value.ToString();
var cExcel = sh.Cells[28, 2].Value.ToString();
var dExcel = sh.Cells[29, 2].Value.ToString();
var eExcel = sh.Cells[30, 2].Value.ToString();
var fExcel = sh.Cells[31, 2].Value.ToString();
double test1 = Convert.ToDouble(aExcel);
double test2 = Convert.ToDouble(bExcel);
double test3 = Convert.ToDouble(cExcel);
double test4 = Convert.ToDouble(dExcel);
double test5 = Convert.ToDouble(aExcel);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can check like this:

 if(sh.Cells[26, 2] != null)
{
//do something
}

code:

double test;
var aExcel = sh.Cells[26, 2];
if(aExcel != null){
test = Convert.ToDouble(aExcel.Value.ToString());
}
 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading