简体   繁体   中英

Unhandled exception when sequence contains no elements

I am publishing an application using VS2012 that connects and reads data from a local MDB file. The application runs fine in Windows 7; however, when I attempt to run it in XP I get an unhandled exception.

I have looked around on here and found a couple pages in regards to this; however, I guess I am not understanding what the actual cause is. I copied the page that I have found to be the most relevant but not sure how to implement it in my model.

Sequence contains no elements?

Below is the code that I am using to query the DB. Any suggestions on what I could be doing wrong?

conection.Open();
var query = "select t_id From t_user where u_company='"+profselect.Text+"'";
var command = new System.Data.OleDb.OleDbCommand(query, conection);
var reader = command.ExecuteReader();
string blah=(reader[0].ToString());
textBox1.Text = blah;
reader.Close();
conection.Close();

You need to call the Read() function of the reader to actually get to the record(s):

var reader = command.ExecuteReader();
while (reader.Read()) {
  string blah=(reader[0].ToString());
  textBox1.Text = blah;
}

or if expecting only one row:

var reader = command.ExecuteReader();
if (reader.Read()) {
  string blah=(reader[0].ToString());
  textBox1.Text = blah;
} else {
  textBox1.Text = "oops.  no record";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM