Solved

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

Posted on 2004-10-25
3,155 Views
Last Modified: 2013-11-19
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
0
Question by:ptslv
    8 Comments
     
    LVL 7

    Expert Comment

    by:rohanbairat3
    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
     
    LVL 15

    Accepted Solution

    by:
    Add
    using System.Data.OleDb;
    0
     
    LVL 8

    Assisted Solution

    by:Razzie_
    you need to add the following using statement:

    using System.Data.OleDb;

    and it will work.

    Cheers,

    Razzie
    0
     

    Author Comment

    by:ptslv
    They are part of the public class Form1
    0
     

    Author Comment

    by:ptslv
    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
     
    LVL 15

    Expert Comment

    by:Timbo87
    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
     
    LVL 8

    Expert Comment

    by:Razzie_
    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
     

    Author Comment

    by:ptslv
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now