Question

Looking for an hint on this strange problem.

I'm trying to retrieve data from a access (OleDb) db into a c#/wpf DataTable. Now the funny thing is: a simple query works in access and is downloaded into the Datatable, just fine. A (slightly) more complex query works in Access but gives an error in c#.

Here's the code:

 OleDbConnection conn = new OleDbConnection("myconn");
 DataTable dt = new DataTable();
 OleDbCommand cmd = new OleDbCommand();
 OleDbDataAdapter adapter = new OleDbDataAdapter();

 conn.Open();

       //3 flavors          
       //string n = "QryTestComplex";        This gives an error in c#, works in access
       //string n = "SELECT * FROM CURSIST"; This works ( with cmd.CommandType =  
                                                                   etc, commented out)
       //string n = "QryTestSimple";         This works   ( SQL = same as above )

cmd = new OleDbCommand(n,conn);
cmd.CommandType = CommandType.StoredProcedure;

adapter.SelectCommand = cmd
            try
            {
                adapter.Fill(dt);
                int t = 0;
                foreach (DataRow dr in dt.Rows)
                { t++; Console.WriteLine(t + dr[0].ToString()); }
            }
            catch (Exception exc)
            {
            Console.WriteLine("{0} Exception caught.", exc);
            }

            conn.Close();

The queries are:

QryTestSimple:

SELECT * FROM Cursist;

QryTestComplex:

SELECT 
    Module.Status, 
    ModuleData.Datum, 
    LeerActiviteit.Naam, 
    Module.Locatie, 
    Module.Onderwijskundige, 
    Module.Organisator, 
    Module.Coordinator, 
    Module.Stoplicht, 
    Module.ID, Cluster.Naam AS CNaam, 
    (
        SELECT '(dag' & count(M.ID) + 1 & ')'  
        FROM  
            Module AS M  
            INNER JOIN ModuleData AS MD 
                ON M.ID = MD.Module_ID 
        WHERE Module.ID = M.ID 
            AND MD.Datum <ModuleData.Datum  
        HAVING  
            (
                SELECT count(P.ID) 
                FROM 
                    Module AS P 
                    INNER JOIN 
                    ModuleData AS PD 
                        ON P.ID = PD.Module_ID  
                WHERE Module.ID= P.ID 
            ) >1 
    ) AS DagNummer
FROM 
    (
        LeerActiviteit 
        INNER JOIN 
        (
            [Module] 
            LEFT JOIN 
            Cluster 
                ON Module.Onderdeel_Van_Cluster_ID = Cluster.ID
        ) 
            ON LeerActiviteit.ID = Module.LeerAct_ID
    ) 
    INNER JOIN 
    ModuleData 
        ON Module.ID = ModuleData.Module_ID
WHERE (((ModuleData.Datum)>Now()));

The Complex query works in access, but when debugging in C# I get:

System.Data.OleDb.OleDbException (0x80004005): IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

Breaks on adapter.Fill(dt);

Any suggestions?

Thanks,

Arnold

Was it helpful?

Solution

Here's your problem:

cmd.CommandType = CommandType.StoredProcedure;

T'ain't no such thing in Access. If you're reading a query as is, make it .Table. If you're composing a select statement, make it .Text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top