Getting Error: type or namespace name 'OleDbConnection'/'OleDbAdapter' could not be found

I am building a Windows App in C# and connecting to an Access database to insert data.  I have:

using System.Data;
using System.Text;
using System.Xml;

private OleDbConnection oleDbConnection;
private OleDbDataAdapter oleDbDataAdapter;

in my app but am getting the following error on the OleDbConnection and OleDbAdapter:

Error:  The type or namespace name 'OleDbConnection'/'OleDbAdapter' could not be found (are you missing a using directive or an assembly reference?)

I have the following function:

private void addMyRow(string DocType)
{
string doc = DocType.ToString();
string myConnection1 = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\inetpub\wwwroot\DTS\ElectronicData\ElectronicData.mdb");
OleDbConnection myConnect = new OleDbConnection(myConnection1);

OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Imports",myConnect);

da.Fill(ds,"Imports");            
OleDbCommand myOleDbCommand = OleDbCommand.CreateCommand();
           
bool bIsError=false;
  try
  {
    if( data.m_timeStamp.Length == 0)
      MessageBox.Show("An error occurred while importing this record. There is no timestamp.  Please notify Programmer.");
    else
    {
      switch(doc.ToString())
      {
        case "1":
           string sqlcmd = "Insert into Imports (DocumentName, DocumentType)" +
            " values ('" + data.m_DocumentName + "' + '" + data.m_DocumentType + "')";
           myOleDbCommand.CommandText = sqlcmd;
           DataRow myRow = myTable.NewRow();
             myRow("DocumentName") = data.DocumentName;
           myRow("DocumentType") = data.DocumentType;
             myConnection1.Open();
           myTable.Rows.Add(myRow);
           OleDbDataAdapter1.Append(ds, "Imports");
           oleDbCommand1.ExecuteNonQuery();
             break;
        }
     }
  }
  catch(Exception err)
  {
     string str=err.Message;
     bIsError=true;
  }
  finally
  {
    myConnection1.Close();
  }
  if(!bIsError)
  {
    MessageBox.Show("XML Data has been submited!");
  }
  else
  {
     MessageBox.Show("There has been an error with this page.  Please contact the programmer."
  }
}

Any help is appreciated.

ptslv
ptslvAsked:
Who is Participating?
 
Timbo87Connect With a Mentor Commented:
Add
using System.Data.OleDb;
0
 
rohanbairat3Commented:
private OleDbConnection oleDbConnection;
private OleDbDataAdapter oleDbDataAdapter;

statements like these should be a part of a class or method.

Declare them inside a class or method and u wont get an error.

-rohan
0
 
Razzie_Connect With a Mentor Commented:
you need to add the following using statement:

using System.Data.OleDb;

and it will work.

Cheers,

Razzie
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ptslvAuthor Commented:
They are part of the public class Form1
0
 
ptslvAuthor Commented:
I added: using System.Data.OleDb;
 and made some adjustments and corrections, and get the following errors:

1. myConnect.Open(); (string does not contain a definition for Open() )
2.  myConnect.Close(); (string does not contain a definition for Close() )
 3.  OleDbDataAdapter.Append(ds, "Imports"); ( OleDbDataAdapter does not contain a definition for 'Append')  


0
 
Timbo87Commented:
Replace myConnection1.Open(); with myConnect.Open();
Replace myConnection1.Close(); with myConnect.Close();

I'm not exactly sure what you're trying to do with #3.
0
 
Razzie_Commented:
OleDbDataAdapter does not have an 'Append()' method. Moreover, you use OleDbDataAdapter1 in your code instead of OleDbDataAdapter (notice the extra 1). That can't be right for starters.
0
 
ptslvAuthor Commented:
OK.  Let me go back thru my code and clean it up.  Meanwhile, since I received your responses at the about the same time for the original question, I am splitting the points between Timbo87 and Razzie.  If I have anymore problems I will post a new question.  Thanks!

ptslv
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.