I'm trying to compile some code in Visual C# Express.
Would someone please have a gander and help me out by giving me a brife explanation of why I'm getting an error "oleDbDataAdapter is not in the current context"?

From my understanding (which must be wrong) oleDbDataAdapter is part of System.Data.Oledb context.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
  
    public partial class formProcessor : Form
    {
        public formProcessor()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
// Put all file names in root directory into array.
            string[] array1 = Directory.GetFiles(@"C:\documents and settings\All Users\Documents\IGRT\", "*.jpg");  
// Connect to mdb
            string connetionString = null;
            System.Data.OleDb.OleDbConnection cnn;
            connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\All Users\\Documents\\IGRT\\shifts.mdb;";
            cnn = new System.Data.OleDb.OleDbConnection(connetionString);
            try
            {
                cnn.Open();
                MessageBox.Show("Connection Open ! ");
                cnn.Close();
            }
            catch
            {
                MessageBox.Show("Can not open connection ! ");

            }
 //increment for softName           
            int x = 0;       
            foreach (string name in array1)
            {
                x++;
                string sql = "SELECT * FROM ShiftTable WHERE imgName = \'"+(name)+"\'";
                oleDbDataAdapter.SelectCommand.CommandText;  //getting error message here

Dani AI

Generated

— the compiler error simply means the name oleDbDataAdapter doesn't exist where you use it. is right: you never declare that variable. Also the statement you have (oleDbDataAdapter.SelectCommand.CommandText;) does nothing — it reads a property and discards it. You need to create an adapter (or a command) and either set its SelectCommand or pass the SQL+connection to the adapter constructor, then call Fill() to retrieve results.

Typical fixes and best practices:

  • Declare and instantiate the adapter (or the command) before you use it. C# is case-sensitive; the class is OleDbDataAdapter.
  • Prefer parameterized queries (OleDb uses ? placeholders) instead of string concatenation.
  • Directory.GetFiles() returns full paths. If your DB stores only file names, use Path.GetFileName(fullPath) for the parameter.
  • If you use the Jet provider on a 64-bit machine, compile for x86 or use the ACE provider.

Example pattern (inside your loop — adjusted to use the filename only and a parameterized query):

var fileName = Path.GetFileName(fullPath);           // fullPath comes from Directory.GetFiles()
var sql = "SELECT * FROM ShiftTable WHERE imgName = ?";

using (var cmd = new OleDbCommand(sql, cnn))
{
    cmd.Parameters.AddWithValue("?", fileName);
    using (var adapter = new OleDbDataAdapter(cmd))
    {
        var dt = new DataTable();
        adapter.Fill(dt);
        // handle dt rows
    }
}

If you prefer a shorter form you can also do var adapter = new OleDbDataAdapter(sql, cnn); and then set parameters on adapter.SelectCommand. Checklist if errors persist: ensure using System.Data.OleDb; is present, the adapter variable is declared in the correct scope, there are no earlier compile errors hiding the real issue, and the project platform matches the OLE DB provider.

I don't see anywhere in your code that you declare the variable oleDbDataAdapter, which is what the error is telling you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.