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();
}
}
}
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