I have the following:

string oledbcmd = "";
        string constr =
            @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Excel.xls;@Extended Properties='Excel 8.0;HDR=Yes;";

            oledbcmd = "Select top (1) * From [CLIENTES$] ORDER BY id desc";

        OleDbCommand oconn = new OleDbCommand(oledbcmd, con);
        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);

        DataTable data = new DataTable();
        sda.Fill(data);

It fails. What is wrong with that query? I want to get the last row that was inserted.

Recommended Answers

All 2 Replies

Guess you forgot to open your connection.

commented: Should hae added that it works without the order by and top... -1

Sadly, I cannot edit but I want to make clear that

oledbcmd = "Select * From [CLIENTES$]";

Works. It isnt a connection issue or anything else (the connection code is already implemented but posting a huge chunk of code makes no sense when just one line is the culprit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.