0

I have a probnlem displaying what's in my Database table. I've followed my class instructions but I've been stuck for 2 days without success!

Here is what I've done so far;

string conStr = @"Data Source=C:\Users\secwp_000\documents\visual studio 2012\Projects\Module5\Module5\Orderdatabase.sdf";


SqlCeConnection con = new SqlCeConnection(conStr);
SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Order", con);
SqlCeDataAdapter adapt = new SqlCeDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds, "Order");

foreach (DataTable dt in ds.Tables)
{
    foreach (DataRow row in dt.Rows)
    {
       foreach (DataColumn column in dt.Columns)
       {
            Console.WriteLine(row[column]);
       }
    }
}

When i run the code i get a error on adapt.Fill(ds, "Order"); saying An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll. Which I don't really understand.

EDIT: My DataConnections is;

Data Connections
    Orderdatabase.sdf
        Tables
            Order
                Columns
                    OrderID
                    etc..

Adding output for Nicks solution:

System.Data.SqlServerCe.SqlCeException (0x80004005): There was an error parsing the query. [ Token line number = 1,Token line offset = 21,Token in error = Order ]
   vid System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
   vid System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
   vid System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   vid System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior)
   vid System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(Command Behavior behavior)
   vid System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable [] datatables, Int32 startRecord, Int32 maxRecords, String, srcTable, IDbCommand command, CommandBehavior behavior)
   vid System.Data.Common.DbDataAdapter.Fill(Dataset dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   vid System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   vid Module5.Program.Main(String[] args) i c:\Users\secwp_000\Documents\Visual Studio 2012\Projects\Module5\Module5\Program.cs:rad 54

EDIT: Here is a picture of what it looks like atm. http://oi61.tinypic.com/9pq70l.jpg

20
  • Welcome to Stack Overflow. What is the stacktrace exactly? Please read FAQ, How to Ask and help center as a start.. Nov 7, 2014 at 12:06
  • Thank you! Sorry I've never encountred that word. So I don't know.
    – Oscar
    Nov 7, 2014 at 12:10
  • Try putting a try catch statement around your adapt.fill. In your catch statement put your e into a message box and it will give you more information about your error. Also as a silly little thing: Are you sure that your table Order is with a capital letter in your database? Silly mistake but I've made it many times.
    – Nick Otten
    Nov 7, 2014 at 12:10
  • Unfortunately I can't post images, but in the server explorer under tables the table is named "Order". I don't know what to write after catch, we have barley worked with that.
    – Oscar
    Nov 7, 2014 at 12:15
  • use it like this: try { adapt.fill(ds, "Order"); } catch (Exception e) {Messagebox.Show(e.toString());}
    – Nick Otten
    Nov 7, 2014 at 12:20

2 Answers 2

0

You need to enclose the table name in brackets, as this is a reserved Word:

SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM [Order]", con);

And using SELECT * is never a good idea

0

First of all SELECT * from Order is a huge collection of data, I suggest you try this:

        SqlConnection con = new SqlCeConnection(conStr);
        using(con)
        {
        SqlCommand cmd = new SqlCeCommand("SELECT OrderID FROM Order", con);
        string OrderID = cmd.ExecuteScalar().ToString();
        Console.WriteLine(OrderID);
        }

And if this is successful then go on to execute using foreach to access all rows and columns using SELECT * FROM ORDER

12
  • I get "ExecuteScalar requires an open and available Connection. The connection's current state is Closed." Im guessing the database isnt connected somehow? :S
    – Oscar
    Nov 7, 2014 at 12:17
  • 2
    using (con) { con.Open(); string orderID = cmd.executeScaler().ToString(); }
    – Nick Otten
    Nov 7, 2014 at 12:18
  • @NickOtten With that coderow i get a error on "...string orderID = cmd.ExecuteScalar().ToString();.." saying "An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll"
    – Oscar
    Nov 7, 2014 at 12:21
  • @SandySands That type or namespace couldnt be found. I can only write SqlCeCommand
    – Oscar
    Nov 7, 2014 at 12:30
  • Namespace : System.Data.SqlClient and also add in the Reference Framework and in Reference Extension
    – SanyTiger
    Nov 7, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.