C# Access 2000 file error.

  • Thread starter Thread starter sevenso
  • Start date Start date
S

sevenso

I am having problems coding to an Access database (2000
format). I am using the Microsoft Development Enviroment
2003 and 1.1 .Net Framework.

Here is the code that I am trying:
string strConnect =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\CSharp\Receipt\Test.MDB" ;
string strSQL = "SELECT * FROM Names";

try
{

OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL,
strConnect);


DataSet ds = new DataSet("Names");
adapter.Fill(ds, "Names"); // never gets beyond here

string str = (string)ds.Tables["Names"].Rows[0]
["FirstName"];

DataRow newRow = ds.Tables["Names"].NewRow();
newRow["FirstName"] = "Teddy";
ds.Tables["Names"].Rows.Add(newRow);
ds.Tables["Names"].AcceptChanges();

}
catch (System.Data.OleDb.OleDbException ex)
{
MessageBox.Show(ex.Message);
Debug.WriteLine(ex.Message);
}

When this runs an exception is thrown at adapter.Fill
(...).
The message is: "IErrorInfo.GetDescription failed with
E_FAIL(0x80004005)."

I also can't open the database in the Data Connections
area of the IDE. I get an Unkown error message box.

Thanks for the help.
 
Paul, thanks for the input. I have tried putting the
double slashes in and the result is the same. Any other
ideas?
-----Original Message-----
¤ I am having problems coding to an Access database (2000
¤ format). I am using the Microsoft Development Enviroment
¤ 2003 and 1.1 .Net Framework.
¤
¤ Here is the code that I am trying:
¤ string strConnect =
¤ @"Provider=Microsoft.Jet.OLEDB.4.0;Data
¤ Source=D:\CSharp\Receipt\Test.MDB" ;
¤ string strSQL = "SELECT * FROM Names";
¤

In C# your file path must use double slashes instead of a single slash:

string ConnectionString;
ConnectionString
= "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\CSharp\Receipt\\Test.MDB";
 
Do you have read/write permissions in the D:\Csharp\Receipt directory? You
would need this to open the database. Also have you opened the database
directly with Access? You may want to do a Compact/Repair on the database
if you have Access installed.

Ron Allen
 
¤ Paul, thanks for the input. I have tried putting the
¤ double slashes in and the result is the same. Any other
¤ ideas?

Actually yes I do. Try enclosing the Names table with brackets - it is an ODBC reserved word:

string strSQL = "SELECT * FROM [Names]";


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Back
Top