Why is my SQLCeCommand ExecuteReader failing?

With the following code I get a less-than-helpful err msg (all I can see of the err msg in the truncated title bar of the exception dialog is, "System.Data.SQLServer...")

string query = "SELECT * FROM EVERYTHING";
SqlCeCommand cmd = new SqlCeCommand(query);
SqlCeConnection conn = new SqlCeConnection(myConnStr); 
conn.Open();
cmd.Connection = conn;
SqlCeDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); // <-- Blows up bigger than an Augustus Gloop pinata

UPDATE

I added this:

MessageBox.Show(string.Format("query is {0}", query));

...to do a sanity check on just what the query that was failing was, to the end that clumps of hair are now scattered all about my work area. I had this to feed the query:

string vendorId = txtVendor.ToString().Trim();

...instead of this:

string vendorId = txtVendor.Text.ToString().Trim();

...and thus the query was "SELECT BLA FROM BLA WHERE BLA = System.Windows.Forms.Label"

Now I'm at least to a "No data exists for the row/column" err msg.

Upvotes: 0

Views: 593

Answers (1)

user153923
user153923

Reputation:

I'm not sure if CF supports the CommandBehavior.CloseConnection option.

Can you write it this way?

string query = "SELECT * FROM EVERYTHING";
var table = new DataTable();
using (var cmd = new SqlCeCommand(query, new SqlCeConnection(myConnStr)); {
  try {
    cmd.Connection.Open();
    table.Load(cmd.ExecuteReader());
  } catch (SqlException err) {
    Console.WriteLine(err.Message); // <= Put a Break Point here.
  } finally {
    cmd.Connection.Close();
  } 
}
object col1 = null;
string strCol2 = null;
if (0 < table.Rows.Count) {
  col1 = table.Rows[0][0];
  object obj = table.Rows[0][1];
  if ((obj != null) && (obj != DBNull.Value)) {
    strCol2 = obj.ToString();
  }
}

EDIT: Added DataTable and read 2 items from Row[0].

Upvotes: 1

Related Questions