C# and DAO. Why RS.Fields(“ColName”) doesn't work?
Question
dao.DBEngine DBE;
dao.Database DB;
dao.Recordset RS;
string DBPath;
DBPath = "C:\\Test\\test.mdb";
DBE = new dao.DBEngine();
DB = DBE.OpenDatabase(DBPath, false, false, "");
RS = DB.OpenRecordset("SELECT * FROM TEST", dao.RecordsetTypeEnum.dbOpenSnapshot, 0, dao.LockTypeEnum.dbOptimistic);
RS.MoveLast(0);
RS.MoveFirst();
String Val = RS.Fields("ColName");
Last instruction works in VBA or VB.NET I think, why doesn't it work on C# ?
Solution
try String Val = RS.Fields["ColName"]; C# uses square brackets to reference collections
OTHER TIPS
Use square brackets, explicitly state the property name which in this case is Value and cast it to a string:
string Val = (string)RS.Fields["ColName"].Value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow