public class InnovationSubmission
{
public OleDbConnection connectr = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabaseTEST.accdr;Jet OLEDB:Database Password=DWPOIT");
public OleDbDataAdapter Adaptor = new OleDbDataAdapter();
private List<SelectListItem> _Area = new List<SelectListItem>();
private List<SelectListItem> _Team = new List<SelectListItem>();
public int ID { get; set; }
[Required(ErrorMessage="Please enter a short description")]
public string ShortDescription { get; set; }
[Required(ErrorMessage = "Please enter a detailed description")]
public string DetailedDescription { get; set; }
public string Status { get; set; }
[Required(ErrorMessage = "Please enter your name")]
public string Originator { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+",
ErrorMessage = "Please enter a valid email address")]
public string OriginatorEmail { get; set; }
[Required]
public string SelectedArea { get; set; }
[Required]
public string SelectedTeam { get; set; }
public List<SelectListItem> Area
{
get { return _Area; }
}
public List<SelectListItem> Teams
{
get
{
string SQLSelect = ("SELECT * FROM Teams");
OleDbCommand sCommand = new OleDbCommand(SQLSelect, connectr);
Adaptor.SelectCommand = sCommand;
connectr.Open();
Adaptor.Fill(areaTable);
connectr.Dispose();
foreach (DataRow row in areaTable.Rows)
{
_Team.Add(new SelectListItem() { Text = row[1].ToString(), Value = row[1].ToString() });
}
return _Team;
}
}
public DataTable areaTable = new DataTable();
}
}
To me it looks like you have a problem with the connector object. Probably it's used somewhere else and that's why you get the exception. Open new connection, do the query and close it. Try using using
to achieve that and query within the using block.
Also, as already mentioned in comments - it's not a very good idea to do a db query in a property getter. Looks weird and hacky...