2

I am reading some Data from a Paradox table with Oledb. The Problem I have is the code works when I copy it to a console Application but not in WinForms. Both debug as x86 and I literally just copy the code. In the WinForms App i get the External table is not in the expected format Exception.

My code:

StringBuilder ConnectionString = new StringBuilder();
        OleDbConnection connection = new OleDbConnection();



        ConnectionString.Append("Provider=Microsoft.Jet.OLEDB.4.0;");
        ConnectionString.Append("Data Source=C:\test;");
        ConnectionString.Append("Extended Properties='Paradox 5.x'");
        connection.ConnectionString = ConnectionString.ToString();
        OleDbCommand command = new OleDbCommand();

        string query = "SELECT * FROM NACHKALK WHERE Auftrag='U04-0001'";

        command.CommandText = query;

        command.Connection = connection;

        connection.Open();
        OleDbDataAdapter adp = new OleDbDataAdapter(command);



        adp.Fill(dset);

        connection.Close();

Does anybody know how I can solve this?

Friedlman
  • 87
  • 1
  • 7
  • Are both set to the same version of Net Library? Are both on same PC? – jdweng Aug 24 '17 at 10:40
  • 1
    @jdweng yes both 4.5 and on the same PC – Friedlman Aug 24 '17 at 12:02
  • Did you change the name of the DataSource? The DataSource should be a filename with an extension. – jdweng Aug 24 '17 at 12:15
  • 1
    @jdweng I have it without extension on both projects... Just the path. And it works on the console app – Friedlman Aug 24 '17 at 13:05
  • 1
    @jdweng the strange thing is: when I close my app that uses Paradox tables with bde (in an other folder) both applications work. If I open the application just the console app works. – Friedlman Aug 24 '17 at 13:08
  • Then the Jet is assuming the filename is the same as the table name NACHKALK. Are you using the same file on both PCs. Is the file have the correct table name? Try following : command.CommandType = System.Data.CommandType.TableDirect; (also try CommandType.Text). – jdweng Aug 24 '17 at 13:14
  • A form project in the file Program.cs has following : [STAThread]Main() while a console project doesn't. Now the msdn website says following for oledb : Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. Not sure what this all means. So is the code in the form project in a static method? Try making the connection static. So I think the error may be due to the connection being in an instance of a class which is not static. – jdweng Aug 24 '17 at 13:26
  • @jdweng Thank you. I tried to put the connection from my class to public Form1() and there it works :) but is there a workaround or do I have to keep it there? – Friedlman Aug 24 '17 at 13:47
  • It need to be in a static method. Main in a Console Application is static. Also the startup Form in a Form Application is static since it has [STAThread] in Program.cs. – jdweng Aug 24 '17 at 14:07
  • @jdweng ok now I dont know what is wrong. I changed the code a little bit. In the console application everything works fine. But in the Forms application even in Forms1() I get an error "Invalid operation" at xx.fill(dset) . When I close the bde Paradox application both of them work – Friedlman Aug 24 '17 at 14:24
  • If you open the form project before the console application do you get same results? – jdweng Aug 24 '17 at 15:11
  • You say that with the BDE shut down, both applications work without error -- can you get both your Forms application and your console application to run at the same time? – A. I. Breveleri Aug 25 '17 at 18:17

0 Answers0