2

Using C#, I am attempting to query an Access db (.accdb). My code, below, works fine until it gets to the line that actually tries to look at the value contained in a field: Console.WriteLine(rs.Fields.Item(0).Value);) in main().

I have searched on the warning message:

'ADODB.Fields' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument type of 'ADODB.Fields' could be found (are you missing a using directive or an assembly reference?)

As well, I have searched on "C# ADODB How to reference individual fields" Everything that comes up in those searches indicates that 'Item' should be a member of the 'ADODB.Recordset.Fields' namespace; or shows me how to use the iterator to iterate over each field in the current record, which is not what I want.

I have the "using ADODB;" directive in my code, as well as the "adodb" reference: (my reputation level does not allow me to post the screen clip I prepared, so I guess you'll have to take my word for it.)

How do I reference an individual field in the Fields collection? (preferably by name, but I can also live with index)

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ADODB;
using iTextSharp;


namespace HistAssessPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdCreatePDFs_Click(object sender, EventArgs e)
        {
            main();
        }

        private void main()
        {
            // throw new NotImplementedException();
            string strConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                "Data Source='C:\\MyLocalDirectory\\MyAccessdb.accdb';" + 
                                "Persist Security Info=False;"; 

            Connection db = new Connection();
            db.Open(strConnStr);

            Recordset rs = new Recordset();

            rs.ActiveConnection = db;
            rs.CursorType = CursorTypeEnum.adOpenForwardOnly;
            rs.LockType = LockTypeEnum.adLockReadOnly;
            rs.Open("select LAST_NAME from ClientTbl;");
            while (!rs.EOF)
            {
                Console.WriteLine(rs.Fields.Item("LAST_NAME").Value); //also tried rs.Fields.Item(0).Value
                rs.MoveNext();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, EventArgs e)
        {
            // Code in here to clean things up (sever connection to db, destroy objects, etc.) before actually exiting the app
            MessageBox.Show("I'm about to close.");
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
johncroc
  • 37
  • 1
  • 11
  • 1
    you are trying to access the field name incorrectly from what I can see. try something like this `rs.fields["LAST_NAME"].ToString()` – MethodMan Aug 27 '14 at 18:03
  • I would look at using this technique with OleDB here are some good working examples in the answers section of this `SO` Previous posting http://stackoverflow.com/questions/930/how-do-i-connect-to-a-database-and-loop-over-a-recordset-in-c – MethodMan Aug 27 '14 at 18:07
  • To whoever down-voted my question: Could you please help me by making suggestions on how I could improve my question? – johncroc Aug 27 '14 at 18:08
  • @DJKRAZE - That's it! Thank you so much. If you'll be so kind as to put your solution in as an answer (instead of a comment), I'll mark it as the answer so you get credit for it. – johncroc Aug 27 '14 at 18:13
  • Oops. I spoke too soon. Adding '.Value' instead of '.ToString()', returns the actual contents of the field. Thank you so much. If you'll be so kind as to put your solution in as an answer (instead of a comment), I'll mark it as the answer so you get credit for it. – johncroc Aug 27 '14 at 18:20
  • I added the comment as an answer.. thanks – MethodMan Aug 27 '14 at 19:59

1 Answers1

1

you are trying to access the field name incorrectly from what I can see. try something like this

rs.fields["LAST_NAME"].ToString(); //this should do the trick 

or

rs.fields["LAST_NAME"].Value; should work as well
MethodMan
  • 18,625
  • 6
  • 34
  • 52