0

I wrote this code (in C# .net using Arcobjcts) to read record from my table:

 ITable table =featureWorkspace1.OpenTable(featureClass1.AliasName);
 List<String> list2 = new List<String>();
 ITableSelection tableSelection = table as ITableSelection;
 ISelectionSet2 selectionSet = tableSelection.SelectionSet as ISelectionSet2;
 ICursor Cursor = null;
 IFeatureCursor featureCursor = Cursor as IFeatureCursor;
 selectionSet.Search(null, true, out Cursor);
 int fieldIndex = featureCursor.Fields.FindField(champ);
 list2.Add(fieldIndex.ToString());
 this.listBox2.DataSource = list2;

But an error occurs:

an object reference not set to an instance of an object

Also I tried:

 IQueryFilter queryFilter = new QueryFilterClass();
 queryFilter.WhereClause = " champ <> '' ";
 ICursor      Cursor = table.Search(queryFilter,true);
 IRow city = Cursor.NextRow();
 while (city != null)
 {
     list2.Add((String)(city.Value[fieldIndex].ToString()));
     city = Cursor.NextRow();
 }    
 this.listBox2.DataSource = list2;

The error was:

Exception from HRESULT 0x80040358

Can you please help me how to fix one of those two problems?

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
user3644700
  • 1
  • 1
  • 1
  • which line do you get your exception on? Also, if you're not checking succession of casting, you shouldn't use `as` operator. – Tarec May 16 '14 at 13:04

1 Answers1

1

Check the value in city.Value[fieldIndex] and make sure it is != System.DBNull.Value before calling .ToString() on it.

I would also verify that your field index is correct and that the field you are querying for champ is spelled correctly.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Tim Sexton
  • 188
  • 8