BOB.G
BOB.G

Reputation: 83

How to Check for Empty data entry before populating ListBox

private void LoadExcelSheet(string path, int sheet){
    _Application excel = new Excel.Application();
    Workbook wb;
    Worksheet ws;
    string data = "";
    int row = 0;
    int col = 0;

    wb = excel.Workbooks.Open(path);
    ws = wb.Worksheets[sheet];
    listBox1.Items.Clear();

    for (row = 1; row < 10; row++){ 
        data = " ";
        for (col = 1; col < 3; col++) {
            data += ws.Cells[row, col].Value2 + " "; 
        }

       //wanted to filter out empty cells/data and at the same time count
       //number of items in the list... row should stop.. I think!

       if(data == null){
          break;
       }

    listBox1.Items.Add(data);  
}

The if statement doesn't seems to work no matter what I do. I would appreciate it very if anyone could point me in the right direction.

Upvotes: 1

Views: 58

Answers (2)

Bhagyesh Patel
Bhagyesh Patel

Reputation: 171

Add a condition like this

If(string.IsNullOrEmpty(data))
{
  Break;
}

Upvotes: 0

Vijunav Vastivch
Vijunav Vastivch

Reputation: 4191

use it like this:

 if (data.Trim().Length < 1)
  {
      return;
  }

use return not break

Upvotes: 1

Related Questions