0
Answer

Not able to import a specific excel cell to a textbox C#

Tejas Kullarwar

Tejas Kullarwar

11y
1.9k
1
Hi I am trying to import value of a specific excel cell to a textbox on the form. The value will appear as soon as the form loads. Now this cell can be null and I am taking care of the null value but it is throwing an exception and going into catch everytime a null value is passed. Here is the snippet of my code:

try
{
var v;
if
( oSheet.Cells[3, 4].Value2 != null)
{v = Sheet.Cells[3, 4].Value2; 
var s = Convert.ToString(v);
textBox1 = string.Format("{0:N2}", s);
}
else { v = 0;
 var s = Convert.ToString(v);
 textBox1 = string.Format("{0:N2}", v);
 } 
}
catch(Exception)

{

string errorMessage = "Data cannot be retrieved from the strats file ";MessageBox.Show(errorMessage);
}

The catch is showing this exception "object reference not set to an instance of an object"