raym0nd
raym0nd

Reputation: 3262

SqlCeDataReader is always null

Hey Guys I'm working on Windows Mobile 5.0 (.Net 2.0) and I'm stuck at a small problem where my reader is always being null. My connection is fine and I believe everything else is fine the only problem is with the reader. In my exception catch it says

((System.Data.SqlServerCe.SqlCeException)(e)) :  {"The operation completed successfully."}
InnerException: Could not evaluate expression

My database file is not corrupt I opened it outside the application and everything looks fine. My code is as follows:

  public static List<Image> GetAll(BOI caller)
    {
        List<Image> images = new List<Image>();
        SqlCeConnection c = Connection.GetConnection(caller.ConnectionString);

        if (c != null)
        {
            SqlCeCommand command = new SqlCeCommand("SELECT * FROM Images", c);

            SqlCeDataReader reader = null;
            try
            {
                reader = command.ExecuteReader();  <<<<< reader is null <<<<<<<
            }
            catch (Exception e)
            {
                while (reader != null && reader.Read())
                {
                    Image temp = new Image((int)reader["imageKey"],
                              (String)reader["filename"],
                              (byte[])reader["image"],
                              (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]),
                              (int)reader["priority"]);
                    temp.SetDBName(caller.ConnectionString);

                    images.Add(temp);
                }
            }
        }

        return images;
    }

EDIT I open my connection in Connection.GetConnection(..);

EDIT:2 The e.StackTrace:

   at System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr)
   at System.Data.SqlServerCe.SqlCeDataReader.FillMetaData(SqlCeCommand command)
   at System.Data.SqlServerCe.SqlCeCommand.InitializeDataReader(SqlCeDataReader reader, Int32 resultType)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader()
   at Oralys.BOI.DataAccess.ImageMapper.GetAll(BOI caller)
   at Oralys.BOI.BOI.get_Images()
   at Oralys.BOI.BOI_Controller.FetchAllImages()
   at IdeoVoiceMobile.RunProfile.InitBOI()
   at IdeoVoiceMobile.RunProfile..ctor()
   at IdeoVoiceMobile.Program.startProfile()
   at IdeoVoiceMobile.Program.Main()

Get Connection function:

    public static SqlCeConnection GetConnection(string connectionString)
    {
        SqlCeConnection conn = null;
        try
        {
            if (connections.Count == 0)
            {
                OpenConnection(connectionString);
            }
            conn = connections[connectionString];
        }
        catch (System.Exception)
        {
        }
        return conn;
    }

EDIT:3 Exception code when using

SqlCeCommand command = new SqlCeCommand("SELECT * FROM Images Where imageKey=6", c);

ExceptionCode: 0xc0000005
ExceptionAddress: 0x0115438c
Reading: 0x00000000
Faulting module: sqlceqp35.dll
Offset: 0x0001438c

   at NativeMethods.GetKeyInfo(IntPtr pIUnknown, IntPtr pTx, String pwszBaseTable, IntPtr prgDbKeyInfo, Int32 cDbKeyInfo, IntPtr pError)
   at SqlCeDataReader.FillMetaData(SqlCeCommand command)
   at SqlCeCommand.InitializeDataReader(SqlCeDataReader reader, Int32 resultType)
   at SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
   at SqlCeCommand.ExecuteReader(CommandBehavior behavior)
   at SqlCeCommand.ExecuteReader()
   at ImageMapper.GetAll(BOI caller)
   at BOI.get_Images()
   at BOI_Controller.FetchAllImages()
   at RunProfile.InitBOI()
   at RunProfile..ctor()
   at Program.startProfile()
   at Program.Main()

Upvotes: 1

Views: 1778

Answers (3)

raym0nd
raym0nd

Reputation: 3262

The problem ended up to be because I was using System.Data.SqlServerCe 3.5.1.0 when I switched to 3.5.0.0 it worked without any error. And I put the while loop outside the catch statement.

But I'm still annoyed from MS because they didnt show any error regarding that!

Upvotes: 1

user153923
user153923

Reputation:

Raym0nd, I'm guessing you are new at this.

Take a look at this version of what you provided.

public static List<Image> GetAll(BOI caller) {
  List<Image> images = new List<Image>();
  using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Images", Connection.GetConnection(caller.ConnectionString))) {
    try {
      cmd.Connection.Open();
      using (SqlCeReader reader = cmd.ExecuteReader()) {
        while (reader.Read()) {
          object imageKey = reader["imageKey"];
          object filename = reader["filename"];
          object image = reader["image"];
          object labelKey = reader["labelKey"];
          object priority = reader["priority"];
          Image img = new Image((interface)imageKey, (string)filename, (byte[])image, (int?)labelKey, (int)priority);
          img.SetDBName(caller.ConnectionString);
          images.Add(img);
        }
      }
    } catch (SqlCeException err) {
      Console.WriteLine(err.Message);
      throw err;
    } finally {
      cmd.Connection.Close();
    }
  }
  return images;
}

You'll want to add some functionality to test your objects in the case of your nullable integer, but for the most part is is clean and simple.

Put a break point on the Console.WriteLine(), and mouse over the Message if an exception occurs.

Upvotes: 0

Adam
Adam

Reputation: 456

You'll never hit the following line of code unless cm.executereader throws an exception:

while (reader != null && reader.Read())

Try moving the below code outside of the catch statement

            while (reader != null && reader.Read())
            {
                Image temp = new Image((int)reader["imageKey"],
                          (String)reader["filename"],
                          (byte[])reader["image"],
                          (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]),
                          (int)reader["priority"]);
                temp.SetDBName(caller.ConnectionString);

                images.Add(temp);
            }

Upvotes: 2

Related Questions