1

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();
Community
  • 1
  • 1
  • So its the same code that works find on when system and not on another? Is it referring to the same MDB file? – Chris Aug 08 '13 at 20:19
  • yeah same file, and location no different code. I published to a website and installed it from the same location using the two different OS's. Windows 7 is good and XP falls over – Shane Superfly MacNeill Aug 08 '13 at 20:24

1 Answers1

3

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";
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225